2024-11-25 19:46:18 +09:00
package hodu
import "encoding/json"
import "net/http"
2024-12-05 01:26:44 +09:00
import "net/url"
2024-11-26 09:41:15 +09:00
import "strconv"
2024-11-25 19:46:18 +09:00
/ *
* POST GET PUT DELETE
* / servers - create new server list all servers bulk update delete all servers
* / servers / 1 - X get server 1 details update server 1 delete server 1
* / servers / 1 / xxx -
2024-11-28 01:29:02 +09:00
*
2024-11-25 22:55:03 +09:00
* / servers / 1112123 / peers
2024-11-28 01:29:02 +09:00
* POST add a new peer to a server
* GET list all peers
* PUT create / replace
* PATCH partial update
2024-11-30 00:19:39 +09:00
* / servers / 1112123 / peers / 1231344
* GET get info
2024-11-25 19:46:18 +09:00
* /
2024-11-26 09:41:15 +09:00
type json_errmsg struct {
2024-11-25 22:55:03 +09:00
Text string ` json:"error-text" `
}
2024-11-25 19:46:18 +09:00
2024-11-30 02:53:47 +09:00
type json_in_client_conn struct {
ServerAddr string ` json:"server-addr" `
}
type json_in_client_route struct {
ClientPeerAddr string ` json:"client-peer-addr" `
}
type json_out_client_conn_id struct {
Id uint32 ` json:"id" `
2024-11-26 09:41:15 +09:00
}
2024-11-30 00:19:39 +09:00
type json_out_client_conn struct {
2024-11-28 01:29:02 +09:00
Id uint32 ` json:"id" `
2024-12-03 11:52:46 +09:00
ReqServerAddr string ` json:"req-server-addr" ` // server address requested. may be a domain name
ServerAddr string ` json:"server-addr" ` // actual server address
2024-12-02 02:19:50 +09:00
ClientAddr string ` json:"client-addr" `
2024-11-30 00:19:39 +09:00
Routes [ ] json_out_client_route ` json:"routes" `
2024-11-28 01:29:02 +09:00
}
2024-11-30 02:53:47 +09:00
type json_out_client_route_id struct {
Id uint32 ` json:"id" `
}
2024-11-30 00:19:39 +09:00
type json_out_client_route struct {
2024-11-28 01:29:02 +09:00
Id uint32 ` json:"id" `
2024-11-30 00:19:39 +09:00
ClientPeerAddr string ` json:"client-peer-addr" `
2024-11-28 01:29:02 +09:00
ServerPeerListenAddr string ` json:"server-peer-listen-addr" `
}
2024-11-30 20:06:59 +09:00
type json_out_client_peer struct {
Id uint32 ` json:"id" `
ClientPeerAddr string ` json:"client-peer-addr" `
2024-12-01 17:20:16 +09:00
ClientLocalAddr string ` json:"client-local-addr" `
2024-11-30 20:06:59 +09:00
ServerPeerAddr string ` json:"server-peer-addr" `
2024-12-01 17:20:16 +09:00
ServerLocalAddr string ` json:"server-local-addr" `
2024-11-30 20:06:59 +09:00
}
2024-11-28 01:29:02 +09:00
// ------------------------------------
2024-11-30 00:19:39 +09:00
type client_ctl_client_conns struct {
2024-11-25 19:46:18 +09:00
c * Client
}
2024-11-30 00:19:39 +09:00
type client_ctl_client_conns_id struct {
2024-11-25 19:46:18 +09:00
c * Client
}
2024-11-30 00:19:39 +09:00
type client_ctl_client_conns_id_routes struct {
2024-11-26 09:41:15 +09:00
c * Client
}
2024-11-30 00:19:39 +09:00
type client_ctl_client_conns_id_routes_id struct {
c * Client
}
2024-11-30 20:06:59 +09:00
type client_ctl_client_conns_id_routes_id_peers struct {
c * Client
}
type client_ctl_client_conns_id_routes_id_peers_id struct {
c * Client
}
2024-11-30 00:19:39 +09:00
2024-11-25 22:55:03 +09:00
// ------------------------------------
2024-11-25 19:46:18 +09:00
2024-11-30 00:19:39 +09:00
func ( ctl * client_ctl_client_conns ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
2024-11-25 19:46:18 +09:00
var c * Client
2024-11-25 22:55:03 +09:00
var status_code int
2024-11-25 19:46:18 +09:00
var err error
2024-11-30 00:19:39 +09:00
var je * json . Encoder
2024-11-25 19:46:18 +09:00
c = ctl . c
2024-11-30 00:19:39 +09:00
je = json . NewEncoder ( w )
2024-11-25 19:46:18 +09:00
switch req . Method {
case http . MethodGet :
2024-11-25 22:55:03 +09:00
var cts * ClientConn
2024-11-30 00:19:39 +09:00
var js [ ] json_out_client_conn
2024-12-05 01:26:44 +09:00
var q url . Values
q = req . URL . Query ( )
// TODO: brief listing vs full listing
if q . Get ( "brief" ) == "true" {
}
2024-11-28 01:29:02 +09:00
2024-11-30 00:19:39 +09:00
js = make ( [ ] json_out_client_conn , 0 )
2024-11-25 22:55:03 +09:00
c . cts_mtx . Lock ( )
2024-11-30 18:59:36 +09:00
for _ , cts = range c . cts_map {
2024-11-28 01:29:02 +09:00
var r * ClientRoute
2024-11-30 00:19:39 +09:00
var jsp [ ] json_out_client_route
2024-11-28 01:29:02 +09:00
2024-11-30 00:19:39 +09:00
jsp = make ( [ ] json_out_client_route , 0 )
2024-11-28 01:29:02 +09:00
cts . route_mtx . Lock ( )
for _ , r = range cts . route_map {
2024-11-30 00:19:39 +09:00
jsp = append ( jsp , json_out_client_route {
2024-11-28 01:29:02 +09:00
Id : r . id ,
2024-11-30 02:53:47 +09:00
ClientPeerAddr : r . peer_addr ,
2024-11-28 01:29:02 +09:00
ServerPeerListenAddr : r . server_peer_listen_addr . String ( ) ,
} )
}
2024-12-02 02:19:50 +09:00
js = append ( js , json_out_client_conn {
Id : cts . id ,
2024-12-03 11:52:46 +09:00
ReqServerAddr : cts . cfg . ServerAddr ,
2024-12-02 02:19:50 +09:00
ServerAddr : cts . remote_addr ,
ClientAddr : cts . local_addr ,
Routes : jsp ,
} )
2024-11-28 01:29:02 +09:00
cts . route_mtx . Unlock ( )
2024-11-25 22:55:03 +09:00
}
c . cts_mtx . Unlock ( )
2024-11-28 01:29:02 +09:00
2024-11-30 00:19:39 +09:00
status_code = http . StatusOK ; w . WriteHeader ( status_code )
2024-11-28 01:29:02 +09:00
if err = je . Encode ( js ) ; err != nil { goto oops }
2024-11-25 19:46:18 +09:00
case http . MethodPost :
2024-11-30 15:38:06 +09:00
// add a new client connection to a server with no client-side peers
// while the client code can accept one more more client-side peer address
// with the server added, i don't intentially accept peer addresses
// as it's tricky to handle erroneous cases in creating the client routes
// after hacing connected to the server. therefore, the json_in_client_conn
// type contains a server address field only.
2024-11-30 02:53:47 +09:00
var s json_in_client_conn
2024-11-25 19:46:18 +09:00
var cc ClientConfig
2024-11-25 22:55:03 +09:00
var cts * ClientConn
2024-11-25 19:46:18 +09:00
err = json . NewDecoder ( req . Body ) . Decode ( & s )
2024-12-03 00:55:19 +09:00
if err != nil || s . ServerAddr == "" {
2024-11-30 00:19:39 +09:00
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
2024-11-25 22:55:03 +09:00
goto done
2024-11-25 19:46:18 +09:00
}
2024-12-03 00:55:19 +09:00
2024-11-25 19:46:18 +09:00
cc . ServerAddr = s . ServerAddr
2024-11-30 02:53:47 +09:00
//cc.PeerAddrs = s.PeerAddrs
2024-11-25 22:55:03 +09:00
cts , err = c . start_service ( & cc ) // TODO: this can be blocking. do we have to resolve addresses before calling this? also not good because resolution succeed or fail at each attempt. however ok as ServeHTTP itself is in a goroutine?
if err != nil {
status_code = http . StatusInternalServerError ; w . WriteHeader ( status_code )
2024-11-26 09:41:15 +09:00
if err = je . Encode ( json_errmsg { Text : err . Error ( ) } ) ; err != nil { goto oops }
2024-11-25 22:55:03 +09:00
} else {
status_code = http . StatusCreated ; w . WriteHeader ( status_code )
2024-11-30 02:53:47 +09:00
if err = je . Encode ( json_out_client_conn_id { Id : cts . id } ) ; err != nil { goto oops }
2024-11-25 22:55:03 +09:00
}
2024-11-25 19:46:18 +09:00
case http . MethodDelete :
2024-11-30 18:59:36 +09:00
// delete all client connections to servers. if we request to stop all
// client connections, they will remove themselves from the client.
// we do passive deletion rather than doing active deletion by calling
// c.RemoveAllClientConns()
c . ReqStopAllClientConns ( )
2024-11-30 00:19:39 +09:00
status_code = http . StatusNoContent ; w . WriteHeader ( status_code )
2024-11-25 22:55:03 +09:00
default :
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
2024-11-25 19:46:18 +09:00
}
2024-11-25 22:55:03 +09:00
done :
// TODO: need to handle x-forwarded-for and other stuff? this is not a real web service, though
c . log . Write ( "" , LOG_DEBUG , "[%s] %s %s %d" , req . RemoteAddr , req . Method , req . URL . String ( ) , status_code ) // TODO: time taken
2024-11-25 19:46:18 +09:00
return
2024-11-25 22:55:03 +09:00
oops :
c . log . Write ( "" , LOG_ERROR , "[%s] %s %s - %s" , req . RemoteAddr , req . Method , req . URL . String ( ) , err . Error ( ) )
2024-11-25 19:46:18 +09:00
return
}
2024-11-25 22:55:03 +09:00
// ------------------------------------
2024-11-25 19:46:18 +09:00
2024-11-30 00:19:39 +09:00
// client-conns/{conn_id}
func ( ctl * client_ctl_client_conns_id ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
var c * Client
var status_code int
var err error
var conn_id string
var conn_nid uint64
var je * json . Encoder
2024-12-03 00:55:19 +09:00
var cts * ClientConn
2024-11-30 00:19:39 +09:00
c = ctl . c
je = json . NewEncoder ( w )
conn_id = req . PathValue ( "conn_id" )
conn_nid , err = strconv . ParseUint ( conn_id , 10 , 32 )
if err != nil {
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "wrong connection id - " + conn_id } ) ; err != nil { goto oops }
goto done
}
2024-12-03 00:55:19 +09:00
cts = c . FindClientConnById ( uint32 ( conn_nid ) )
if cts == nil {
status_code = http . StatusNotFound ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "non-existent connection id - " + conn_id } ) ; err != nil { goto oops }
goto done
}
2024-11-25 22:55:03 +09:00
switch req . Method {
case http . MethodGet :
2024-11-30 00:19:39 +09:00
var r * ClientRoute
var jsp [ ] json_out_client_route
var js * json_out_client_conn
jsp = make ( [ ] json_out_client_route , 0 )
cts . route_mtx . Lock ( )
for _ , r = range cts . route_map {
jsp = append ( jsp , json_out_client_route {
Id : r . id ,
2024-11-30 02:53:47 +09:00
ClientPeerAddr : r . peer_addr ,
2024-11-30 00:19:39 +09:00
ServerPeerListenAddr : r . server_peer_listen_addr . String ( ) ,
} )
}
2024-12-03 11:52:46 +09:00
js = & json_out_client_conn {
Id : cts . id ,
ReqServerAddr : cts . cfg . ServerAddr ,
ServerAddr : cts . local_addr ,
ClientAddr : cts . remote_addr ,
Routes : jsp ,
}
2024-11-30 00:19:39 +09:00
cts . route_mtx . Unlock ( )
2024-11-25 22:55:03 +09:00
2024-11-30 00:19:39 +09:00
status_code = http . StatusOK ; w . WriteHeader ( status_code )
if err = je . Encode ( js ) ; err != nil { goto oops }
2024-11-25 22:55:03 +09:00
case http . MethodDelete :
2024-12-03 00:55:19 +09:00
//c.RemoveClientConn(cts)
cts . ReqStop ( )
status_code = http . StatusNoContent ; w . WriteHeader ( status_code )
2024-11-30 03:08:20 +09:00
2024-11-30 00:19:39 +09:00
default :
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
2024-11-25 22:55:03 +09:00
}
return
2024-11-30 00:19:39 +09:00
done :
// TODO: need to handle x-forwarded-for and other stuff? this is not a real web service, though
c . log . Write ( "" , LOG_DEBUG , "[%s] %s %s %d" , req . RemoteAddr , req . Method , req . URL . String ( ) , status_code ) // TODO: time taken
return
oops :
c . log . Write ( "" , LOG_ERROR , "[%s] %s %s - %s" , req . RemoteAddr , req . Method , req . URL . String ( ) , err . Error ( ) )
2024-11-25 22:55:03 +09:00
return
2024-11-25 19:46:18 +09:00
}
2024-11-30 18:59:36 +09:00
// ------------------------------------
2024-11-30 00:19:39 +09:00
func ( ctl * client_ctl_client_conns_id_routes ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
2024-11-26 09:41:15 +09:00
var c * Client
var status_code int
var err error
2024-11-30 00:19:39 +09:00
var conn_id string
var conn_nid uint64
var je * json . Encoder
var cts * ClientConn
2024-11-26 09:41:15 +09:00
c = ctl . c
2024-11-30 00:19:39 +09:00
je = json . NewEncoder ( w )
conn_id = req . PathValue ( "conn_id" )
conn_nid , err = strconv . ParseUint ( conn_id , 10 , 32 )
if err != nil {
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "wrong connection id - " + conn_id } ) ; err != nil { goto oops }
goto done
}
cts = c . FindClientConnById ( uint32 ( conn_nid ) )
if cts == nil {
status_code = http . StatusNotFound ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "non-existent connection id - " + conn_id } ) ; err != nil { goto oops }
goto done
}
2024-11-26 09:41:15 +09:00
switch req . Method {
case http . MethodGet :
2024-11-30 00:19:39 +09:00
var r * ClientRoute
var jsp [ ] json_out_client_route
jsp = make ( [ ] json_out_client_route , 0 )
cts . route_mtx . Lock ( )
for _ , r = range cts . route_map {
jsp = append ( jsp , json_out_client_route {
Id : r . id ,
2024-11-30 02:53:47 +09:00
ClientPeerAddr : r . peer_addr ,
2024-11-30 00:19:39 +09:00
ServerPeerListenAddr : r . server_peer_listen_addr . String ( ) ,
} )
}
cts . route_mtx . Unlock ( )
status_code = http . StatusOK ; w . WriteHeader ( status_code )
if err = je . Encode ( jsp ) ; err != nil { goto oops }
2024-11-26 09:41:15 +09:00
case http . MethodPost :
2024-11-30 02:53:47 +09:00
var jcr json_in_client_route
var r * ClientRoute
2024-11-26 09:41:15 +09:00
2024-11-30 02:53:47 +09:00
err = json . NewDecoder ( req . Body ) . Decode ( & jcr )
2024-12-05 01:26:44 +09:00
if err != nil || jcr . ClientPeerAddr == "" {
2024-11-26 09:41:15 +09:00
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
goto done
}
2024-11-30 02:53:47 +09:00
r , err = cts . AddNewClientRoute ( jcr . ClientPeerAddr , ROUTE_PROTO_TCP ) // TODO: configurable protocol
if err != nil {
status_code = http . StatusInternalServerError ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : err . Error ( ) } ) ; err != nil { goto oops }
2024-11-26 09:41:15 +09:00
} else {
2024-11-30 02:53:47 +09:00
status_code = http . StatusCreated ; w . WriteHeader ( status_code )
if err = je . Encode ( json_out_client_route_id { Id : r . id } ) ; err != nil { goto oops }
2024-11-26 09:41:15 +09:00
}
2024-11-30 02:53:47 +09:00
case http . MethodDelete :
2024-11-30 18:59:36 +09:00
//cts.RemoveAllClientRoutes()
cts . ReqStopAllClientRoutes ( )
2024-11-30 02:53:47 +09:00
status_code = http . StatusNoContent ; w . WriteHeader ( status_code )
2024-11-26 09:41:15 +09:00
default :
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
}
done :
// TODO: need to handle x-forwarded-for and other stuff? this is not a real web service, though
c . log . Write ( "" , LOG_DEBUG , "[%s] %s %s %d" , req . RemoteAddr , req . Method , req . URL . String ( ) , status_code ) // TODO: time taken
return
oops :
c . log . Write ( "" , LOG_ERROR , "[%s] %s %s - %s" , req . RemoteAddr , req . Method , req . URL . String ( ) , err . Error ( ) )
return
2024-11-28 01:29:02 +09:00
2024-11-26 09:41:15 +09:00
}
2024-11-30 00:19:39 +09:00
// ------------------------------------
func ( ctl * client_ctl_client_conns_id_routes_id ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
var c * Client
var status_code int
var err error
var conn_id string
var route_id string
var conn_nid uint64
var route_nid uint64
var je * json . Encoder
var cts * ClientConn
c = ctl . c
je = json . NewEncoder ( w )
conn_id = req . PathValue ( "conn_id" )
route_id = req . PathValue ( "route_id" )
conn_nid , err = strconv . ParseUint ( conn_id , 10 , 32 )
if err != nil {
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "wrong connection id - " + conn_id } ) ; err != nil { goto oops }
goto done
}
route_nid , err = strconv . ParseUint ( route_id , 10 , 32 )
if err != nil {
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "wrong route id - " + route_id } ) ; err != nil { goto oops }
goto done
}
cts = c . FindClientConnById ( uint32 ( conn_nid ) )
if cts == nil {
status_code = http . StatusNotFound ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "non-existent connection id - " + conn_id } ) ; err != nil { goto oops }
goto done
}
switch req . Method {
case http . MethodGet :
var r * ClientRoute
r = cts . FindClientRouteById ( uint32 ( route_nid ) )
if r == nil {
status_code = http . StatusNotFound ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "non-existent route id - " + conn_id } ) ; err != nil { goto oops }
goto done
}
err = je . Encode ( json_out_client_route {
Id : r . id ,
2024-11-30 02:53:47 +09:00
ClientPeerAddr : r . peer_addr ,
2024-11-30 00:19:39 +09:00
ServerPeerListenAddr : r . server_peer_listen_addr . String ( ) ,
} )
if err != nil { goto oops }
case http . MethodDelete :
2024-12-01 19:11:12 +09:00
cts . ReqStop ( )
status_code = http . StatusNoContent ; w . WriteHeader ( status_code )
2024-11-30 00:19:39 +09:00
default :
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
}
done :
// TODO: need to handle x-forwarded-for and other stuff? this is not a real web service, though
c . log . Write ( "" , LOG_DEBUG , "[%s] %s %s %d" , req . RemoteAddr , req . Method , req . URL . String ( ) , status_code ) // TODO: time taken
return
oops :
c . log . Write ( "" , LOG_ERROR , "[%s] %s %s - %s" , req . RemoteAddr , req . Method , req . URL . String ( ) , err . Error ( ) )
return
}
2024-11-25 22:55:03 +09:00
// ------------------------------------
2024-11-25 19:46:18 +09:00
2024-11-30 20:06:59 +09:00
func ( ctl * client_ctl_client_conns_id_routes_id_peers ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
var c * Client
var status_code int
var err error
var conn_id string
var route_id string
var conn_nid uint64
var route_nid uint64
var je * json . Encoder
var r * ClientRoute
c = ctl . c
je = json . NewEncoder ( w )
conn_id = req . PathValue ( "conn_id" )
route_id = req . PathValue ( "route_id" )
conn_nid , err = strconv . ParseUint ( conn_id , 10 , 32 )
if err != nil {
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "wrong connection id - " + conn_id } ) ; err != nil { goto oops }
goto done
}
route_nid , err = strconv . ParseUint ( route_id , 10 , 32 )
if err != nil {
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "wrong route id - " + route_id } ) ; err != nil { goto oops }
goto done
}
r = c . FindClientRouteById ( uint32 ( conn_nid ) , uint32 ( route_nid ) )
if r == nil {
status_code = http . StatusNotFound ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "non-existent connection/route id - " + conn_id + "/" + route_id } ) ; err != nil { goto oops }
goto done
}
switch req . Method {
case http . MethodGet :
var p * ClientPeerConn
var jcp [ ] json_out_client_peer
jcp = make ( [ ] json_out_client_peer , 0 )
r . ptc_mtx . Lock ( )
for _ , p = range r . ptc_map {
jcp = append ( jcp , json_out_client_peer {
Id : p . conn_id ,
ClientPeerAddr : p . conn . RemoteAddr ( ) . String ( ) ,
2024-12-01 17:20:16 +09:00
ClientLocalAddr : p . conn . LocalAddr ( ) . String ( ) ,
ServerPeerAddr : p . pts_raddr ,
ServerLocalAddr : p . pts_laddr ,
2024-11-30 20:06:59 +09:00
} )
}
r . ptc_mtx . Unlock ( )
status_code = http . StatusOK ; w . WriteHeader ( status_code )
if err = je . Encode ( jcp ) ; err != nil { goto oops }
2024-12-01 19:11:12 +09:00
case http . MethodDelete :
r . ReqStopAllClientPeerConns ( )
status_code = http . StatusNoContent ; w . WriteHeader ( status_code )
2024-11-30 20:06:59 +09:00
default :
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
}
done :
c . log . Write ( "" , LOG_DEBUG , "[%s] %s %s %d" , req . RemoteAddr , req . Method , req . URL . String ( ) , status_code ) // TODO: time taken
return
oops :
c . log . Write ( "" , LOG_ERROR , "[%s] %s %s - %s" , req . RemoteAddr , req . Method , req . URL . String ( ) , err . Error ( ) )
return
}
// ------------------------------------
func ( ctl * client_ctl_client_conns_id_routes_id_peers_id ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
var c * Client
var status_code int
var err error
var conn_id string
var route_id string
2024-12-01 17:20:16 +09:00
var peer_id string
2024-11-30 20:06:59 +09:00
var conn_nid uint64
var route_nid uint64
2024-12-01 17:20:16 +09:00
var peer_nid uint64
2024-11-30 20:06:59 +09:00
var je * json . Encoder
2024-12-01 17:20:16 +09:00
var p * ClientPeerConn
2024-11-30 20:06:59 +09:00
c = ctl . c
je = json . NewEncoder ( w )
conn_id = req . PathValue ( "conn_id" )
route_id = req . PathValue ( "route_id" )
2024-12-01 17:20:16 +09:00
peer_id = req . PathValue ( "peer_id" )
2024-11-30 20:06:59 +09:00
conn_nid , err = strconv . ParseUint ( conn_id , 10 , 32 )
if err != nil {
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "wrong connection id - " + conn_id } ) ; err != nil { goto oops }
goto done
}
route_nid , err = strconv . ParseUint ( route_id , 10 , 32 )
if err != nil {
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "wrong route id - " + route_id } ) ; err != nil { goto oops }
goto done
}
2024-12-01 17:20:16 +09:00
peer_nid , err = strconv . ParseUint ( peer_id , 10 , 32 )
if err != nil {
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
if err = je . Encode ( json_errmsg { Text : "wrong peer id - " + peer_id } ) ; err != nil { goto oops }
goto done
}
2024-11-30 20:06:59 +09:00
2024-12-01 17:20:16 +09:00
p = c . FindClientPeerConnById ( uint32 ( conn_nid ) , uint32 ( route_nid ) , uint32 ( peer_nid ) )
if p == nil {
2024-11-30 20:06:59 +09:00
status_code = http . StatusNotFound ; w . WriteHeader ( status_code )
2024-12-01 17:20:16 +09:00
if err = je . Encode ( json_errmsg { Text : "non-existent connection/route/peer id - " + conn_id + "/" + route_id + "/" + peer_id } ) ; err != nil { goto oops }
2024-11-30 20:06:59 +09:00
goto done
}
switch req . Method {
case http . MethodGet :
2024-12-01 17:20:16 +09:00
var jcp * json_out_client_peer
jcp = & json_out_client_peer {
Id : p . conn_id ,
ClientPeerAddr : p . conn . RemoteAddr ( ) . String ( ) ,
ClientLocalAddr : p . conn . LocalAddr ( ) . String ( ) ,
ServerPeerAddr : p . pts_raddr ,
ServerLocalAddr : p . pts_laddr ,
}
status_code = http . StatusOK ; w . WriteHeader ( status_code )
if err = je . Encode ( jcp ) ; err != nil { goto oops }
2024-12-01 19:11:12 +09:00
case http . MethodDelete :
p . ReqStop ( )
status_code = http . StatusNoContent ; w . WriteHeader ( status_code )
2024-11-30 20:06:59 +09:00
default :
status_code = http . StatusBadRequest ; w . WriteHeader ( status_code )
}
done :
c . log . Write ( "" , LOG_DEBUG , "[%s] %s %s %d" , req . RemoteAddr , req . Method , req . URL . String ( ) , status_code ) // TODO: time taken
return
oops :
c . log . Write ( "" , LOG_ERROR , "[%s] %s %s - %s" , req . RemoteAddr , req . Method , req . URL . String ( ) , err . Error ( ) )
return
}