2024-11-23 12:30:23 +09:00
package hodu
2024-11-12 22:59:37 +09:00
import "context"
import "crypto/tls"
2024-11-17 14:57:56 +09:00
import "errors"
2024-11-12 22:59:37 +09:00
import "fmt"
import "io"
import "math/rand"
import "net"
2024-11-21 01:11:01 +09:00
import "net/http"
2024-12-01 21:47:11 +09:00
import "os"
2024-11-12 22:59:37 +09:00
import "sync"
import "sync/atomic"
import "google.golang.org/grpc"
2024-11-13 02:20:25 +09:00
//import "google.golang.org/grpc/metadata"
2024-11-12 22:59:37 +09:00
import "google.golang.org/grpc/peer"
import "google.golang.org/grpc/stats"
const PTS_LIMIT = 8192
2024-11-25 19:46:18 +09:00
type ServerConnMap = map [ net . Addr ] * ServerConn
2024-11-12 22:59:37 +09:00
type ServerPeerConnMap = map [ uint32 ] * ServerPeerConn
type ServerRouteMap = map [ uint32 ] * ServerRoute
type Server struct {
2024-11-24 20:39:51 +09:00
ctx context . Context
ctx_cancel context . CancelFunc
2024-11-12 22:59:37 +09:00
tlscfg * tls . Config
2024-11-24 20:39:51 +09:00
2024-11-18 22:25:59 +09:00
wg sync . WaitGroup
2024-11-30 20:24:30 +09:00
ext_mtx sync . Mutex
2024-11-23 12:30:23 +09:00
ext_svcs [ ] Service
2024-11-18 22:25:59 +09:00
stop_req atomic . Bool
2024-11-24 20:39:51 +09:00
stop_chan chan bool
2024-11-18 22:25:59 +09:00
2024-12-01 21:47:11 +09:00
ctl_prefix string
ctl_mux * http . ServeMux
2024-11-23 12:30:23 +09:00
ctl * http . Server // control server
2024-11-21 01:11:01 +09:00
l [ ] * net . TCPListener // main listener for grpc
2024-11-12 22:59:37 +09:00
l_wg sync . WaitGroup
cts_mtx sync . Mutex
2024-11-25 19:46:18 +09:00
cts_map ServerConnMap
2024-11-18 22:25:59 +09:00
cts_wg sync . WaitGroup
2024-11-12 22:59:37 +09:00
gs * grpc . Server
2024-11-21 01:11:01 +09:00
log Logger
2024-11-20 00:31:14 +09:00
2024-11-12 22:59:37 +09:00
UnimplementedHoduServer
}
2024-11-25 19:46:18 +09:00
// connection from client.
2024-11-12 22:59:37 +09:00
// client connect to the server, the server accept it, and makes a tunnel request
2024-11-25 19:46:18 +09:00
type ServerConn struct {
2024-11-20 00:31:14 +09:00
svr * Server
2024-12-02 02:19:50 +09:00
id uint32
2024-11-20 00:31:14 +09:00
caddr net . Addr // client address that created this structure
2024-12-02 02:19:50 +09:00
local_addr net . Addr
2024-11-20 00:31:14 +09:00
pss * GuardedPacketStreamServer
2024-11-12 22:59:37 +09:00
route_mtx sync . Mutex
2024-11-18 22:25:59 +09:00
route_map ServerRouteMap
route_wg sync . WaitGroup
2024-11-12 22:59:37 +09:00
2024-11-20 00:31:14 +09:00
wg sync . WaitGroup
stop_req atomic . Bool
stop_chan chan bool
2024-11-12 22:59:37 +09:00
}
type ServerRoute struct {
2024-11-25 19:46:18 +09:00
cts * ServerConn
2024-11-20 00:31:14 +09:00
l * net . TCPListener
laddr * net . TCPAddr
2024-12-02 02:19:50 +09:00
ptc_addr string
2024-11-20 00:31:14 +09:00
id uint32
2024-11-12 22:59:37 +09:00
pts_mtx sync . Mutex
pts_map ServerPeerConnMap
pts_limit int
pts_last_id uint32
2024-11-20 00:31:14 +09:00
pts_wg sync . WaitGroup
stop_req atomic . Bool
2024-11-18 22:25:59 +09:00
}
type GuardedPacketStreamServer struct {
mtx sync . Mutex
//pss Hodu_PacketStreamServer
Hodu_PacketStreamServer // let's embed it to avoid reimplement Recv() and Context()
2024-11-12 22:59:37 +09:00
}
// ------------------------------------
2024-11-18 22:25:59 +09:00
func ( g * GuardedPacketStreamServer ) Send ( data * Packet ) error {
// while Recv() on a stream is called from the same gorountine all the time,
// Send() is called from multiple places. let's guard it as grpc-go
// doesn't provide concurrency safety in this case.
// https://github.com/grpc/grpc-go/blob/master/Documentation/concurrency.md
g . mtx . Lock ( )
defer g . mtx . Unlock ( )
return g . Hodu_PacketStreamServer . Send ( data )
}
/ *
func ( g * GuardedPacketStreamServer ) Recv ( ) ( * Packet , error ) {
return g . pss . Recv ( )
}
func ( g * GuardedPacketStreamServer ) Context ( ) context . Context {
return g . pss . Context ( )
} * /
// ------------------------------------
2024-12-02 02:19:50 +09:00
func NewServerRoute ( cts * ServerConn , id uint32 , proto ROUTE_PROTO , ptc_addr string ) ( * ServerRoute , error ) {
2024-11-18 22:25:59 +09:00
var r ServerRoute
var l * net . TCPListener
var laddr * net . TCPAddr
var err error
2024-11-23 14:49:04 +09:00
l , laddr , err = cts . make_route_listener ( proto )
2024-11-18 22:25:59 +09:00
if err != nil {
return nil , err
}
r . cts = cts
r . id = id
r . l = l
r . laddr = laddr
2024-12-02 02:19:50 +09:00
r . ptc_addr = ptc_addr
2024-11-18 22:25:59 +09:00
r . pts_limit = PTS_LIMIT
r . pts_map = make ( ServerPeerConnMap )
r . pts_last_id = 0
r . stop_req . Store ( false )
2024-11-23 14:49:04 +09:00
return & r , nil
2024-11-18 22:25:59 +09:00
}
2024-11-12 22:59:37 +09:00
func ( r * ServerRoute ) AddNewServerPeerConn ( c * net . TCPConn ) ( * ServerPeerConn , error ) {
var pts * ServerPeerConn
var ok bool
var start_id uint32
r . pts_mtx . Lock ( )
defer r . pts_mtx . Unlock ( )
if len ( r . pts_map ) >= r . pts_limit {
return nil , fmt . Errorf ( "peer-to-server connection table full" )
}
start_id = r . pts_last_id
for {
_ , ok = r . pts_map [ r . pts_last_id ]
if ! ok {
break
}
r . pts_last_id ++
if r . pts_last_id == start_id {
// unlikely to happen but it cycled through the whole range.
return nil , fmt . Errorf ( "failed to assign peer-to-server connection id" )
}
}
pts = NewServerPeerConn ( r , c , r . pts_last_id )
r . pts_map [ pts . conn_id ] = pts
r . pts_last_id ++
return pts , nil
}
func ( r * ServerRoute ) RemoveServerPeerConn ( pts * ServerPeerConn ) {
r . pts_mtx . Lock ( )
delete ( r . pts_map , pts . conn_id )
r . pts_mtx . Unlock ( )
2024-11-23 14:49:04 +09:00
r . cts . svr . log . Write ( "" , LOG_DEBUG , "Removed server-side peer connection %s" , pts . conn . RemoteAddr ( ) . String ( ) )
2024-11-12 22:59:37 +09:00
}
2024-11-18 22:25:59 +09:00
func ( r * ServerRoute ) RunTask ( wg * sync . WaitGroup ) {
2024-11-12 22:59:37 +09:00
var err error
var conn * net . TCPConn
var pts * ServerPeerConn
2024-11-23 17:20:53 +09:00
var log_id string
2024-11-12 22:59:37 +09:00
2024-11-18 22:25:59 +09:00
defer wg . Done ( )
2024-11-23 17:20:53 +09:00
log_id = fmt . Sprintf ( "%s,%d" , r . cts . caddr . String ( ) , r . id )
2024-11-18 22:25:59 +09:00
2024-11-12 22:59:37 +09:00
for {
conn , err = r . l . AcceptTCP ( )
if err != nil {
2024-11-17 14:57:56 +09:00
if errors . Is ( err , net . ErrClosed ) {
2024-11-24 20:39:51 +09:00
r . cts . svr . log . Write ( log_id , LOG_INFO , "Server-side peer listener closed" )
2024-11-17 14:57:56 +09:00
} else {
2024-11-23 17:20:53 +09:00
r . cts . svr . log . Write ( log_id , LOG_INFO , "Server-side peer listener error - %s" , err . Error ( ) )
2024-11-17 14:57:56 +09:00
}
2024-11-12 22:59:37 +09:00
break
}
pts , err = r . AddNewServerPeerConn ( conn )
if err != nil {
2024-11-23 17:20:53 +09:00
r . cts . svr . log . Write ( log_id , LOG_ERROR , "Failed to add new server-side peer %s - %s" , conn . RemoteAddr ( ) . String ( ) , err . Error ( ) )
2024-11-12 22:59:37 +09:00
conn . Close ( )
} else {
2024-11-23 17:20:53 +09:00
r . cts . svr . log . Write ( log_id , LOG_DEBUG , "Added new server-side peer %s" , conn . RemoteAddr ( ) . String ( ) )
2024-11-12 22:59:37 +09:00
r . pts_wg . Add ( 1 )
2024-11-18 22:25:59 +09:00
go pts . RunTask ( & r . pts_wg )
2024-11-12 22:59:37 +09:00
}
}
2024-11-13 23:14:43 +09:00
2024-11-24 20:39:51 +09:00
r . ReqStop ( )
2024-11-18 22:25:59 +09:00
r . pts_wg . Wait ( )
2024-11-23 17:20:53 +09:00
r . cts . svr . log . Write ( log_id , LOG_DEBUG , "All service-side peer handlers completed" )
2024-11-24 20:39:51 +09:00
r . cts . RemoveServerRoute ( r ) // final phase...
2024-11-12 22:59:37 +09:00
}
2024-11-18 22:25:59 +09:00
func ( r * ServerRoute ) ReqStop ( ) {
2024-11-25 19:46:18 +09:00
fmt . Printf ( "requesting to stop route taak..\n" )
2024-11-18 22:25:59 +09:00
if r . stop_req . CompareAndSwap ( false , true ) {
var pts * ServerPeerConn
for _ , pts = range r . pts_map {
pts . ReqStop ( )
}
2024-11-23 14:49:04 +09:00
r . l . Close ( )
2024-11-18 22:25:59 +09:00
}
2024-11-25 19:46:18 +09:00
fmt . Printf ( "requiested to stopp route taak..\n" )
2024-11-12 22:59:37 +09:00
}
2024-11-25 19:46:18 +09:00
func ( r * ServerRoute ) ReportEvent ( pts_id uint32 , event_type PACKET_KIND , event_data [ ] byte ) error {
2024-11-12 22:59:37 +09:00
var spc * ServerPeerConn
var ok bool
r . pts_mtx . Lock ( )
spc , ok = r . pts_map [ pts_id ]
if ! ok {
2024-11-23 14:49:04 +09:00
r . pts_mtx . Unlock ( )
2024-11-12 22:59:37 +09:00
return fmt . Errorf ( "non-existent peer id - %u" , pts_id )
}
2024-11-23 14:49:04 +09:00
r . pts_mtx . Unlock ( )
2024-11-12 22:59:37 +09:00
return spc . ReportEvent ( event_type , event_data )
}
// ------------------------------------
2024-11-25 19:46:18 +09:00
func ( cts * ServerConn ) make_route_listener ( proto ROUTE_PROTO ) ( * net . TCPListener , * net . TCPAddr , error ) {
2024-11-12 22:59:37 +09:00
var l * net . TCPListener
var err error
var laddr * net . TCPAddr
var port int
var tries int = 0
var nw string
switch proto {
case ROUTE_PROTO_TCP :
nw = "tcp"
case ROUTE_PROTO_TCP4 :
nw = "tcp4"
case ROUTE_PROTO_TCP6 :
nw = "tcp6"
}
for {
port = rand . Intn ( 65535 - 32000 + 1 ) + 32000
laddr , err = net . ResolveTCPAddr ( nw , fmt . Sprintf ( ":%d" , port ) )
if err == nil {
l , err = net . ListenTCP ( nw , laddr ) // make the binding address configurable. support multiple binding addresses???
if err == nil {
fmt . Printf ( "listening .... on ... %d\n" , port )
return l , laddr , nil
}
}
// TODO: implement max retries..
tries ++
if tries >= 1000 {
err = fmt . Errorf ( "unable to allocate port" )
break
}
}
return nil , nil , err
}
2024-12-02 02:19:50 +09:00
func ( cts * ServerConn ) AddNewServerRoute ( route_id uint32 , proto ROUTE_PROTO , ptc_addr string ) ( * ServerRoute , error ) {
2024-11-12 22:59:37 +09:00
var r * ServerRoute
var err error
cts . route_mtx . Lock ( )
2024-11-18 22:25:59 +09:00
if cts . route_map [ route_id ] != nil {
2024-11-12 22:59:37 +09:00
cts . route_mtx . Unlock ( )
2024-11-25 19:46:18 +09:00
return nil , fmt . Errorf ( "existent route id - %d" , route_id )
2024-11-12 22:59:37 +09:00
}
2024-12-02 02:19:50 +09:00
r , err = NewServerRoute ( cts , route_id , proto , ptc_addr )
2024-11-12 22:59:37 +09:00
if err != nil {
cts . route_mtx . Unlock ( )
return nil , err
}
2024-11-23 14:49:04 +09:00
cts . route_map [ route_id ] = r
2024-11-12 22:59:37 +09:00
cts . route_mtx . Unlock ( )
2024-11-18 22:25:59 +09:00
cts . route_wg . Add ( 1 )
go r . RunTask ( & cts . route_wg )
2024-11-12 22:59:37 +09:00
return r , nil
}
2024-11-25 19:46:18 +09:00
func ( cts * ServerConn ) RemoveServerRoute ( route * ServerRoute ) error {
2024-11-24 20:39:51 +09:00
var r * ServerRoute
var ok bool
cts . route_mtx . Lock ( )
r , ok = cts . route_map [ route . id ]
2024-11-25 19:46:18 +09:00
if ! ok {
2024-11-24 20:39:51 +09:00
cts . route_mtx . Unlock ( )
2024-11-25 19:46:18 +09:00
return fmt . Errorf ( "non-existent route id - %d" , route . id )
2024-11-24 20:39:51 +09:00
}
2024-11-25 19:46:18 +09:00
if r != route {
2024-11-24 20:39:51 +09:00
cts . route_mtx . Unlock ( )
2024-11-25 19:46:18 +09:00
return fmt . Errorf ( "non-existent route - %d" , route . id )
2024-11-24 20:39:51 +09:00
}
delete ( cts . route_map , route . id )
cts . route_mtx . Unlock ( )
r . ReqStop ( )
return nil
}
2024-11-25 19:46:18 +09:00
func ( cts * ServerConn ) RemoveServerRouteById ( route_id uint32 ) ( * ServerRoute , error ) {
2024-11-12 22:59:37 +09:00
var r * ServerRoute
var ok bool
cts . route_mtx . Lock ( )
2024-11-18 22:25:59 +09:00
r , ok = cts . route_map [ route_id ]
2024-11-25 19:46:18 +09:00
if ! ok {
2024-11-12 22:59:37 +09:00
cts . route_mtx . Unlock ( )
2024-11-25 19:46:18 +09:00
return nil , fmt . Errorf ( "non-existent route id - %d" , route_id )
2024-11-12 22:59:37 +09:00
}
2024-11-18 22:25:59 +09:00
delete ( cts . route_map , route_id )
2024-11-12 22:59:37 +09:00
cts . route_mtx . Unlock ( )
2024-11-24 20:39:51 +09:00
r . ReqStop ( )
return r , nil
2024-11-12 22:59:37 +09:00
}
2024-11-25 19:46:18 +09:00
func ( cts * ServerConn ) ReportEvent ( route_id uint32 , pts_id uint32 , event_type PACKET_KIND , event_data [ ] byte ) error {
2024-11-12 22:59:37 +09:00
var r * ServerRoute
var ok bool
2024-11-13 23:14:43 +09:00
2024-11-12 22:59:37 +09:00
cts . route_mtx . Lock ( )
2024-11-18 22:25:59 +09:00
r , ok = cts . route_map [ route_id ]
2024-11-12 22:59:37 +09:00
if ( ! ok ) {
cts . route_mtx . Unlock ( )
2024-11-25 19:46:18 +09:00
return fmt . Errorf ( "non-existent route id - %d" , route_id )
2024-11-12 22:59:37 +09:00
}
cts . route_mtx . Unlock ( )
2024-11-13 23:14:43 +09:00
2024-11-12 22:59:37 +09:00
return r . ReportEvent ( pts_id , event_type , event_data )
}
2024-11-25 19:46:18 +09:00
func ( cts * ServerConn ) receive_from_stream ( wg * sync . WaitGroup ) {
2024-11-12 22:59:37 +09:00
var pkt * Packet
var err error
2024-11-13 23:14:43 +09:00
2024-11-20 00:31:14 +09:00
defer wg . Done ( )
for {
2024-11-18 22:25:59 +09:00
pkt , err = cts . pss . Recv ( )
2024-11-17 14:57:56 +09:00
if errors . Is ( err , io . EOF ) {
2024-11-24 20:39:51 +09:00
cts . svr . log . Write ( "" , LOG_INFO , "GRPC stream closed for client %s" , cts . caddr )
2024-11-18 22:25:59 +09:00
goto done
2024-11-12 22:59:37 +09:00
}
if err != nil {
2024-11-24 20:39:51 +09:00
cts . svr . log . Write ( "" , LOG_ERROR , "GRPC stream error for client %s - %s" , cts . caddr , err . Error ( ) )
2024-11-18 22:25:59 +09:00
goto done
2024-11-12 22:59:37 +09:00
}
switch pkt . Kind {
case PACKET_KIND_ROUTE_START :
var x * Packet_Route
var ok bool
x , ok = pkt . U . ( * Packet_Route )
if ok {
2024-11-25 19:46:18 +09:00
var r * ServerRoute
2024-11-24 20:39:51 +09:00
2024-12-02 02:19:50 +09:00
r , err = cts . AddNewServerRoute ( x . Route . RouteId , x . Route . Proto , x . Route . AddrStr )
2024-11-12 22:59:37 +09:00
if err != nil {
2024-11-24 20:39:51 +09:00
cts . svr . log . Write ( "" , LOG_ERROR , "Failed to add server route for client %s peer %s" , cts . caddr , x . Route . AddrStr )
2024-11-12 22:59:37 +09:00
} else {
2024-11-24 20:39:51 +09:00
cts . svr . log . Write ( "" , LOG_INFO , "Added server route(id=%d) for client %s peer %s" , r . id , cts . caddr , x . Route . AddrStr )
2024-11-18 22:25:59 +09:00
err = cts . pss . Send ( MakeRouteStartedPacket ( r . id , x . Route . Proto , r . laddr . String ( ) ) )
2024-11-12 22:59:37 +09:00
if err != nil {
2024-11-24 20:39:51 +09:00
r . ReqStop ( )
cts . svr . log . Write ( "" , LOG_ERROR , "Failed to inform client %s of server route started for peer %s" , cts . caddr , x . Route . AddrStr )
goto done
2024-11-12 22:59:37 +09:00
}
}
} else {
2024-11-24 20:39:51 +09:00
cts . svr . log . Write ( "" , LOG_INFO , "Received invalid packet from %s" , cts . caddr )
// TODO: need to abort this client?
2024-11-12 22:59:37 +09:00
}
case PACKET_KIND_ROUTE_STOP :
var x * Packet_Route
var ok bool
x , ok = pkt . U . ( * Packet_Route )
if ok {
2024-11-25 19:46:18 +09:00
var r * ServerRoute
2024-11-24 20:39:51 +09:00
r , err = cts . RemoveServerRouteById ( x . Route . RouteId )
2024-11-12 22:59:37 +09:00
if err != nil {
2024-11-24 20:39:51 +09:00
cts . svr . log . Write ( "" , LOG_ERROR , "Failed to delete server route(id=%d) for client %s peer %s" , x . Route . RouteId , cts . caddr , x . Route . AddrStr )
2024-11-12 22:59:37 +09:00
} else {
2024-11-24 20:39:51 +09:00
cts . svr . log . Write ( "" , LOG_ERROR , "Deleted server route(id=%d) for client %s peer %s" , x . Route . RouteId , cts . caddr , x . Route . AddrStr )
2024-11-18 22:25:59 +09:00
err = cts . pss . Send ( MakeRouteStoppedPacket ( x . Route . RouteId , x . Route . Proto ) )
2024-11-12 22:59:37 +09:00
if err != nil {
2024-11-24 20:39:51 +09:00
r . ReqStop ( )
cts . svr . log . Write ( "" , LOG_ERROR , "Failed to inform client %s of server route(id=%d) stopped for peer %s" , cts . caddr , x . Route . RouteId , x . Route . AddrStr )
goto done
2024-11-12 22:59:37 +09:00
}
}
} else {
2024-11-24 20:39:51 +09:00
cts . svr . log . Write ( "" , LOG_INFO , "Received invalid packet from %s" , cts . caddr )
// TODO: need to abort this client?
2024-11-12 22:59:37 +09:00
}
2024-11-13 23:14:43 +09:00
2024-11-12 22:59:37 +09:00
case PACKET_KIND_PEER_STARTED :
// the connection from the client to a peer has been established
var x * Packet_Peer
var ok bool
x , ok = pkt . U . ( * Packet_Peer )
if ok {
err = cts . ReportEvent ( x . Peer . RouteId , x . Peer . PeerId , PACKET_KIND_PEER_STARTED , nil )
if err != nil {
// TODO:
2024-11-25 19:46:18 +09:00
fmt . Printf ( "Failed to report PEER_STARTED Event" )
2024-11-12 22:59:37 +09:00
} else {
2024-11-13 23:14:43 +09:00
// TODO:
2024-11-12 22:59:37 +09:00
}
} else {
// TODO
}
2024-11-13 23:14:43 +09:00
2024-11-20 00:48:02 +09:00
case PACKET_KIND_PEER_ABORTED :
fallthrough
2024-11-12 22:59:37 +09:00
case PACKET_KIND_PEER_STOPPED :
// the connection from the client to a peer has been established
var x * Packet_Peer
var ok bool
x , ok = pkt . U . ( * Packet_Peer )
if ok {
err = cts . ReportEvent ( x . Peer . RouteId , x . Peer . PeerId , PACKET_KIND_PEER_STOPPED , nil )
if err != nil {
// TODO:
2024-11-25 19:46:18 +09:00
fmt . Printf ( "Failed to report PEER_STOPPED Event" )
2024-11-12 22:59:37 +09:00
} else {
2024-11-13 23:14:43 +09:00
// TODO:
2024-11-12 22:59:37 +09:00
}
} else {
// TODO
}
case PACKET_KIND_PEER_DATA :
// the connection from the client to a peer has been established
var x * Packet_Data
var ok bool
x , ok = pkt . U . ( * Packet_Data )
if ok {
err = cts . ReportEvent ( x . Data . RouteId , x . Data . PeerId , PACKET_KIND_PEER_DATA , x . Data . Data )
if err != nil {
// TODO:
2024-11-25 19:46:18 +09:00
fmt . Printf ( "Failed to report PEER_DATA Event" )
2024-11-12 22:59:37 +09:00
} else {
2024-11-13 23:14:43 +09:00
// TODO:
2024-11-12 22:59:37 +09:00
}
} else {
// TODO
}
}
2024-11-20 00:31:14 +09:00
}
2024-11-18 22:25:59 +09:00
done :
2024-11-25 19:46:18 +09:00
fmt . Printf ( "************ stream receiver finished....\n" )
2024-11-18 22:25:59 +09:00
}
2024-11-25 19:46:18 +09:00
func ( cts * ServerConn ) RunTask ( wg * sync . WaitGroup ) {
2024-11-18 22:25:59 +09:00
var strm * GuardedPacketStreamServer
var ctx context . Context
defer wg . Done ( )
strm = cts . pss
ctx = strm . Context ( )
2024-11-20 00:31:14 +09:00
// it looks like the only proper way to interrupt the blocking Recv
// call on the grpc streaming server is exit from the service handler
// which is this function invoked from PacketStream().
// there is no cancel function or whatever that can interrupt it.
// so start the Recv() loop in a separte goroutine and let this
// function be the channel waiter only.
// increment on the wait group is for the caller to wait for
// these detached goroutines to finish.
wg . Add ( 1 )
go cts . receive_from_stream ( wg )
2024-11-18 22:25:59 +09:00
for {
// exit if context is done
// or continue
select {
case <- ctx . Done ( ) : // the stream context is done
2024-11-24 20:39:51 +09:00
fmt . Printf ( "grpc server done - %s\n" , ctx . Err ( ) . Error ( ) )
2024-11-18 22:25:59 +09:00
goto done
case <- cts . stop_chan :
2024-11-24 20:39:51 +09:00
// get out of the loop to eventually to exit from
// this handler to let the main grpc server to
// close this specific client connection.
2024-11-18 22:25:59 +09:00
goto done
2024-11-20 00:31:14 +09:00
//default:
2024-11-18 22:25:59 +09:00
// no other case is ready.
// without the default case, the select construct would block
}
}
done :
2024-11-25 19:46:18 +09:00
fmt . Printf ( "^^^^^^^^^^^^^^^^^ waiting for reoute_wg...\n" )
2024-11-24 20:39:51 +09:00
cts . ReqStop ( ) // just in case
2024-11-18 22:25:59 +09:00
cts . route_wg . Wait ( )
2024-11-25 19:46:18 +09:00
fmt . Printf ( "^^^^^^^^^^^^^^^^^ waited for reoute_wg...\n" )
2024-11-18 22:25:59 +09:00
}
2024-11-25 19:46:18 +09:00
func ( cts * ServerConn ) ReqStop ( ) {
2024-11-18 22:25:59 +09:00
if cts . stop_req . CompareAndSwap ( false , true ) {
var r * ServerRoute
for _ , r = range cts . route_map {
r . ReqStop ( )
}
2024-11-24 20:39:51 +09:00
// there is no good way to break a specific connection client to
// the grpc server. while the global grpc server is closed in
// ReqStop() for Server, the individuation connection is closed
// by returing from the grpc handler goroutine. See the comment
2024-11-25 19:46:18 +09:00
// RunTask() for ServerConn.
2024-11-18 22:25:59 +09:00
cts . stop_chan <- true
}
}
// --------------------------------------------------------------------
2024-11-25 19:46:18 +09:00
func ( s * Server ) GetSeed ( ctx context . Context , c_seed * Seed ) ( * Seed , error ) {
2024-11-20 00:31:14 +09:00
var s_seed Seed
// seed exchange is for furture expansion of the protocol
// there is nothing to do much about it for now.
s_seed . Version = HODU_VERSION
s_seed . Flags = 0
2024-11-25 19:46:18 +09:00
// we create no ServerConn structure associated with the connection
2024-11-20 00:31:14 +09:00
// at this phase for the server. it doesn't track the client version and
// features. we delegate protocol selection solely to the client.
return & s_seed , nil
}
2024-11-18 22:25:59 +09:00
func ( s * Server ) PacketStream ( strm Hodu_PacketStreamServer ) error {
var ctx context . Context
var p * peer . Peer
var ok bool
var err error
2024-11-25 19:46:18 +09:00
var cts * ServerConn
2024-11-18 22:25:59 +09:00
ctx = strm . Context ( )
p , ok = peer . FromContext ( ctx )
2024-11-25 19:46:18 +09:00
if ! ok {
2024-11-18 22:25:59 +09:00
return fmt . Errorf ( "failed to get peer from packet stream context" )
}
2024-12-02 02:19:50 +09:00
cts , err = s . AddNewServerConn ( & p . Addr , & p . LocalAddr , strm )
2024-11-18 22:25:59 +09:00
if err != nil {
return fmt . Errorf ( "unable to add client %s - %s" , p . Addr . String ( ) , err . Error ( ) )
2024-11-12 22:59:37 +09:00
}
2024-11-18 22:25:59 +09:00
// Don't detached the cts task as a go-routine as this function
// is invoked as a go-routine by the grpc server.
s . cts_wg . Add ( 1 )
cts . RunTask ( & s . cts_wg )
return nil
2024-11-12 22:59:37 +09:00
}
// ------------------------------------
type ConnCatcher struct {
server * Server
}
func ( cc * ConnCatcher ) TagRPC ( ctx context . Context , info * stats . RPCTagInfo ) context . Context {
2024-11-25 19:46:18 +09:00
return ctx
2024-11-12 22:59:37 +09:00
}
func ( cc * ConnCatcher ) HandleRPC ( ctx context . Context , s stats . RPCStats ) {
}
func ( cc * ConnCatcher ) TagConn ( ctx context . Context , info * stats . ConnTagInfo ) context . Context {
2024-11-23 14:49:04 +09:00
return ctx
2024-11-25 19:46:18 +09:00
//return context.TODO()
2024-11-12 22:59:37 +09:00
}
func ( cc * ConnCatcher ) HandleConn ( ctx context . Context , cs stats . ConnStats ) {
// fmt.Println(ctx.Value("user_id")) // Returns nil, can't access the value
var p * peer . Peer
var ok bool
var addr string
p , ok = peer . FromContext ( ctx )
2024-11-25 19:46:18 +09:00
if ! ok {
2024-11-12 22:59:37 +09:00
addr = ""
} else {
addr = p . Addr . String ( )
}
2024-11-13 02:20:25 +09:00
/ *
2024-11-12 22:59:37 +09:00
md , ok := metadata . FromIncomingContext ( ctx )
fmt . Printf ( "%+v%+v\n" , md , ok )
if ok {
2024-11-13 02:20:25 +09:00
} * /
2024-11-12 22:59:37 +09:00
switch cs . ( type ) {
case * stats . ConnBegin :
fmt . Printf ( "**** client connected - [%s]\n" , addr )
case * stats . ConnEnd :
fmt . Printf ( "**** client disconnected - [%s]\n" , addr )
2024-11-25 19:46:18 +09:00
cc . server . RemoveServerConnByAddr ( p . Addr )
}
2024-11-12 22:59:37 +09:00
}
// wrappedStream wraps around the embedded grpc.ServerStream, and intercepts the RecvMsg and
// SendMsg method call.
type wrappedStream struct {
grpc . ServerStream
}
func ( w * wrappedStream ) RecvMsg ( m any ) error {
2024-11-20 00:31:14 +09:00
//fmt.Printf("Receive a message (Type: %T) at %s\n", m, time.Now().Format(time.RFC3339))
2024-11-12 22:59:37 +09:00
return w . ServerStream . RecvMsg ( m )
}
func ( w * wrappedStream ) SendMsg ( m any ) error {
2024-11-20 00:31:14 +09:00
//fmt.Printf("Send a message (Type: %T) at %v\n", m, time.Now().Format(time.RFC3339))
2024-11-12 22:59:37 +09:00
return w . ServerStream . SendMsg ( m )
}
func newWrappedStream ( s grpc . ServerStream ) grpc . ServerStream {
return & wrappedStream { s }
}
func streamInterceptor ( srv any , ss grpc . ServerStream , _ * grpc . StreamServerInfo , handler grpc . StreamHandler ) error {
// authentication (token verification)
/ *
md , ok := metadata . FromIncomingContext ( ss . Context ( ) )
if ! ok {
return errMissingMetadata
}
if ! valid ( md [ "authorization" ] ) {
return errInvalidToken
}
* /
err := handler ( srv , newWrappedStream ( ss ) )
if err != nil {
fmt . Printf ( "RPC failed with error: %v\n" , err )
}
return err
}
func unaryInterceptor ( ctx context . Context , req any , _ * grpc . UnaryServerInfo , handler grpc . UnaryHandler ) ( any , error ) {
// authentication (token verification)
/ *
md , ok := metadata . FromIncomingContext ( ctx )
if ! ok {
return nil , errMissingMetadata
}
if ! valid ( md [ "authorization" ] ) {
// return nil, errInvalidToken
}
* /
m , err := handler ( ctx , req )
if err != nil {
fmt . Printf ( "RPC failed with error: %v\n" , err )
}
2024-11-21 01:11:01 +09:00
2024-11-12 22:59:37 +09:00
return m , err
}
2024-12-01 21:47:11 +09:00
func NewServer ( ctx context . Context , ctl_addr string , laddrs [ ] string , logger Logger , tlscfg * tls . Config ) ( * Server , error ) {
2024-11-12 22:59:37 +09:00
var s Server
var l * net . TCPListener
var laddr * net . TCPAddr
var err error
var addr string
var gl * net . TCPListener
2024-12-01 21:47:11 +09:00
var cwd string
2024-11-12 22:59:37 +09:00
if len ( laddrs ) <= 0 {
2024-11-24 20:39:51 +09:00
return nil , fmt . Errorf ( "no server addresses provided" )
2024-11-12 22:59:37 +09:00
}
2024-11-24 20:39:51 +09:00
s . ctx , s . ctx_cancel = context . WithCancel ( ctx )
2024-11-21 01:11:01 +09:00
s . log = logger
2024-11-12 22:59:37 +09:00
/* create the specified number of listeners */
s . l = make ( [ ] * net . TCPListener , 0 )
for _ , addr = range laddrs {
2024-11-23 14:49:04 +09:00
laddr , err = net . ResolveTCPAddr ( NET_TYPE_TCP , addr ) // Make this interruptable???
2024-11-12 22:59:37 +09:00
if err != nil {
goto oops
}
l , err = net . ListenTCP ( NET_TYPE_TCP , laddr )
if err != nil {
goto oops
}
s . l = append ( s . l , l )
}
s . tlscfg = tlscfg
2024-11-23 12:30:23 +09:00
s . ext_svcs = make ( [ ] Service , 0 , 1 )
2024-11-25 19:46:18 +09:00
s . cts_map = make ( ServerConnMap ) // TODO: make it configurable...
2024-11-24 20:39:51 +09:00
s . stop_chan = make ( chan bool , 8 )
2024-11-12 22:59:37 +09:00
s . stop_req . Store ( false )
/ *
creds , err := credentials . NewServerTLSFromFile ( data . Path ( "x509/server_cert.pem" ) , data . Path ( "x509/server_key.pem" ) )
if err != nil {
log . Fatalf ( "failed to create credentials: %v" , err )
}
gs = grpc . NewServer ( grpc . Creds ( creds ) )
* /
s . gs = grpc . NewServer (
grpc . UnaryInterceptor ( unaryInterceptor ) ,
grpc . StreamInterceptor ( streamInterceptor ) ,
2024-11-25 19:46:18 +09:00
grpc . StatsHandler ( & ConnCatcher { server : & s } ) ,
2024-11-12 22:59:37 +09:00
) // TODO: have this outside the server struct?
2024-11-25 19:46:18 +09:00
RegisterHoduServer ( s . gs , & s )
2024-11-12 22:59:37 +09:00
2024-12-01 21:47:11 +09:00
s . ctl_prefix = "" // TODO:
s . ctl_mux = http . NewServeMux ( )
cwd , _ = os . Getwd ( )
s . ctl_mux . Handle ( s . ctl_prefix + "/ui/" , http . StripPrefix ( s . ctl_prefix , http . FileServer ( http . Dir ( cwd ) ) ) ) // TODO: proper directory. it must not use the current working directory...
//s.ctl_mux.HandleFunc(s.ctl_prefix + "/ws/tty", websocket.Handler(server_ws_tty).ServeHTTP)
2024-12-02 02:19:50 +09:00
s . ctl_mux . Handle ( s . ctl_prefix + "/ws/tty" , new_server_ctl_ws_tty ( & s ) )
s . ctl_mux . Handle ( s . ctl_prefix + "/server-conns" , & server_ctl_server_conns { s : & s } )
2024-12-01 21:47:11 +09:00
s . ctl = & http . Server {
Addr : ctl_addr ,
Handler : s . ctl_mux ,
// TODO: more settings
}
2024-11-12 22:59:37 +09:00
return & s , nil
oops :
/* TODO: check if gs needs to be closed... */
if gl != nil {
gl . Close ( )
}
for _ , l = range s . l {
l . Close ( )
}
s . l = make ( [ ] * net . TCPListener , 0 )
return nil , err
}
2024-11-18 22:25:59 +09:00
func ( s * Server ) run_grpc_server ( idx int , wg * sync . WaitGroup ) error {
2024-11-12 22:59:37 +09:00
var l * net . TCPListener
var err error
2024-11-23 14:49:04 +09:00
defer wg . Done ( )
2024-11-18 22:25:59 +09:00
2024-11-12 22:59:37 +09:00
l = s . l [ idx ]
// it seems to be safe to call a single grpc server on differnt listening sockets multiple times
2024-11-25 19:46:18 +09:00
s . log . Write ( "" , LOG_ERROR , "Starting GRPC server listening on %s" , l . Addr ( ) . String ( ) )
2024-11-23 14:49:04 +09:00
err = s . gs . Serve ( l )
2024-11-12 22:59:37 +09:00
if err != nil {
2024-11-21 01:11:01 +09:00
if errors . Is ( err , net . ErrClosed ) {
2024-11-25 19:46:18 +09:00
s . log . Write ( "" , LOG_ERROR , "GRPC server listening on %s closed" , l . Addr ( ) . String ( ) )
2024-11-21 01:11:01 +09:00
} else {
2024-11-25 19:46:18 +09:00
s . log . Write ( "" , LOG_ERROR , "Error from GRPC server listening on %s - %s" , l . Addr ( ) . String ( ) , err . Error ( ) )
2024-11-21 01:11:01 +09:00
}
return err
2024-11-12 22:59:37 +09:00
}
return nil
}
2024-11-18 22:25:59 +09:00
func ( s * Server ) RunTask ( wg * sync . WaitGroup ) {
2024-11-12 22:59:37 +09:00
var idx int
2024-11-18 22:25:59 +09:00
defer wg . Done ( )
2024-11-13 02:20:25 +09:00
2024-11-12 22:59:37 +09:00
for idx , _ = range s . l {
s . l_wg . Add ( 1 )
2024-11-18 22:25:59 +09:00
go s . run_grpc_server ( idx , & s . l_wg )
2024-11-12 22:59:37 +09:00
}
2024-11-24 20:39:51 +09:00
// most the work is done by in separate goroutines (s.run_grp_server)
// this loop serves as a placeholder to prevent the logic flow from
// descening down to s.ReqStop()
task_loop :
for {
select {
case <- s . stop_chan :
break task_loop
}
}
s . ReqStop ( )
2024-11-18 22:25:59 +09:00
s . l_wg . Wait ( )
2024-11-23 17:20:53 +09:00
s . log . Write ( "" , LOG_DEBUG , "All GRPC listeners completed" )
2024-11-24 20:39:51 +09:00
2024-11-18 22:25:59 +09:00
s . cts_wg . Wait ( )
2024-11-23 17:20:53 +09:00
s . log . Write ( "" , LOG_DEBUG , "All CTS handlers completed" )
2024-11-21 01:11:01 +09:00
2024-11-20 00:31:14 +09:00
// stop the main grpc server after all the other tasks are finished.
s . gs . Stop ( )
2024-11-12 22:59:37 +09:00
}
2024-11-21 01:11:01 +09:00
func ( s * Server ) RunCtlTask ( wg * sync . WaitGroup ) {
var err error
defer wg . Done ( )
err = s . ctl . ListenAndServe ( )
if errors . Is ( err , http . ErrServerClosed ) {
2024-11-25 19:46:18 +09:00
fmt . Printf ( "------------http server error - %s\n" , err . Error ( ) )
2024-11-21 01:11:01 +09:00
} else {
2024-11-25 19:46:18 +09:00
fmt . Printf ( "********* http server ended\n" )
2024-11-21 01:11:01 +09:00
}
}
2024-11-12 22:59:37 +09:00
func ( s * Server ) ReqStop ( ) {
if s . stop_req . CompareAndSwap ( false , true ) {
var l * net . TCPListener
2024-11-25 19:46:18 +09:00
var cts * ServerConn
2024-11-12 22:59:37 +09:00
2024-11-25 19:46:18 +09:00
if s . ctl != nil {
2024-11-24 20:39:51 +09:00
// shutdown the control server if ever started.
s . ctl . Shutdown ( s . ctx )
}
2024-11-12 22:59:37 +09:00
//s.gs.GracefulStop()
2024-11-20 00:31:14 +09:00
//s.gs.Stop()
2024-11-12 22:59:37 +09:00
for _ , l = range s . l {
l . Close ( )
}
s . cts_mtx . Lock ( ) // TODO: this mya create dead-lock. check possibility of dead lock???
for _ , cts = range s . cts_map {
cts . ReqStop ( ) // request to stop connections from/to peer held in the cts structure
}
s . cts_mtx . Unlock ( )
2024-11-24 20:39:51 +09:00
s . stop_chan <- true
s . ctx_cancel ( )
2024-11-12 22:59:37 +09:00
}
}
2024-12-02 02:19:50 +09:00
func ( s * Server ) AddNewServerConn ( remote_addr * net . Addr , local_addr * net . Addr , pss Hodu_PacketStreamServer ) ( * ServerConn , error ) {
2024-11-25 19:46:18 +09:00
var cts ServerConn
2024-11-12 22:59:37 +09:00
var ok bool
cts . svr = s
2024-11-18 22:25:59 +09:00
cts . route_map = make ( ServerRouteMap )
2024-12-02 02:19:50 +09:00
cts . caddr = * remote_addr
cts . local_addr = * local_addr
2024-11-18 22:25:59 +09:00
cts . pss = & GuardedPacketStreamServer { Hodu_PacketStreamServer : pss }
2024-11-12 22:59:37 +09:00
cts . stop_req . Store ( false )
2024-11-24 20:39:51 +09:00
cts . stop_chan = make ( chan bool , 8 )
2024-11-12 22:59:37 +09:00
s . cts_mtx . Lock ( )
defer s . cts_mtx . Unlock ( )
2024-12-02 02:19:50 +09:00
_ , ok = s . cts_map [ cts . caddr ]
2024-11-12 22:59:37 +09:00
if ok {
2024-12-02 02:19:50 +09:00
return nil , fmt . Errorf ( "existing client - %s" , cts . caddr . String ( ) )
2024-11-12 22:59:37 +09:00
}
2024-12-02 02:19:50 +09:00
s . cts_map [ cts . caddr ] = & cts
s . log . Write ( "" , LOG_DEBUG , "Added client connection from %s" , cts . caddr . String ( ) )
2024-11-12 22:59:37 +09:00
return & cts , nil
}
2024-12-02 09:46:10 +09:00
func ( s * Server ) ReqStopAllServerConns ( ) {
var cts * ServerConn
s . cts_mtx . Lock ( )
defer s . cts_mtx . Unlock ( )
for _ , cts = range s . cts_map {
cts . ReqStop ( )
}
}
2024-11-25 19:46:18 +09:00
func ( s * Server ) RemoveServerConn ( cts * ServerConn ) {
2024-11-12 22:59:37 +09:00
s . cts_mtx . Lock ( )
delete ( s . cts_map , cts . caddr )
2024-11-23 14:49:04 +09:00
s . log . Write ( "" , LOG_DEBUG , "Removed client connection from %s" , cts . caddr . String ( ) )
2024-11-12 22:59:37 +09:00
s . cts_mtx . Unlock ( )
}
2024-11-25 19:46:18 +09:00
func ( s * Server ) RemoveServerConnByAddr ( addr net . Addr ) {
var cts * ServerConn
2024-11-12 22:59:37 +09:00
var ok bool
s . cts_mtx . Lock ( )
defer s . cts_mtx . Unlock ( )
cts , ok = s . cts_map [ addr ]
if ok {
delete ( s . cts_map , cts . caddr )
2024-12-02 02:19:50 +09:00
cts . ReqStop ( )
2024-11-12 22:59:37 +09:00
}
}
2024-11-25 19:46:18 +09:00
func ( s * Server ) FindServerConnByAddr ( addr net . Addr ) * ServerConn {
var cts * ServerConn
2024-11-12 22:59:37 +09:00
var ok bool
s . cts_mtx . Lock ( )
defer s . cts_mtx . Unlock ( )
cts , ok = s . cts_map [ addr ]
if ! ok {
2024-11-13 23:14:43 +09:00
return nil
2024-11-12 22:59:37 +09:00
}
return cts
}
2024-11-23 12:30:23 +09:00
func ( s * Server ) StartService ( cfg interface { } ) {
s . wg . Add ( 1 )
go s . RunTask ( & s . wg )
}
2024-11-12 22:59:37 +09:00
2024-11-23 12:30:23 +09:00
func ( s * Server ) StartExtService ( svc Service , data interface { } ) {
2024-11-30 20:24:30 +09:00
s . ext_mtx . Lock ( )
2024-11-23 12:30:23 +09:00
s . ext_svcs = append ( s . ext_svcs , svc )
2024-11-30 20:24:30 +09:00
s . ext_mtx . Unlock ( )
2024-11-23 12:30:23 +09:00
s . wg . Add ( 1 )
go svc . RunTask ( & s . wg )
}
2024-11-12 22:59:37 +09:00
2024-12-01 21:47:11 +09:00
func ( s * Server ) StartCtlService ( ) {
s . wg . Add ( 1 )
go s . RunCtlTask ( & s . wg )
}
2024-11-23 12:30:23 +09:00
func ( s * Server ) StopServices ( ) {
var ext_svc Service
s . ReqStop ( )
for _ , ext_svc = range s . ext_svcs {
ext_svc . StopServices ( )
2024-11-12 22:59:37 +09:00
}
2024-11-23 12:30:23 +09:00
}
2024-11-12 22:59:37 +09:00
2024-11-23 12:30:23 +09:00
func ( s * Server ) WaitForTermination ( ) {
2024-11-13 02:20:25 +09:00
s . wg . Wait ( )
2024-11-12 22:59:37 +09:00
}
2024-11-23 14:49:04 +09:00
2024-11-25 19:46:18 +09:00
func ( s * Server ) WriteLog ( id string , level LogLevel , fmtstr string , args ... interface { } ) {
2024-11-23 20:13:07 +09:00
s . log . Write ( id , level , fmtstr , args ... )
2024-11-23 14:49:04 +09:00
}