-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
292 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type statusServer struct { | ||
} | ||
|
||
func (s *statusServer) run(ctx context.Context) error { | ||
srv := &http.Server{ | ||
Addr: ":19182", | ||
Handler: http.HandlerFunc(s.serve), | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
srv.Close() | ||
}() | ||
|
||
return srv.ListenAndServe() | ||
} | ||
|
||
func (s *statusServer) serve(w http.ResponseWriter, r *http.Request) { | ||
if err := s.serveErr(w, r); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprintf(w, "server error: %v", err.Error()) | ||
} | ||
} | ||
|
||
func (s *statusServer) serveErr(w http.ResponseWriter, _ *http.Request) error { | ||
w.Header().Add("Content-Type", "application/json") | ||
enc := json.NewEncoder(w) | ||
return enc.Encode(status{}) | ||
} | ||
|
||
type status struct { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package control | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/connet-dev/connet/model" | ||
"github.com/segmentio/ksuid" | ||
) | ||
|
||
type statusServer struct { | ||
clients *clientServer | ||
relays *relayServer | ||
logger *slog.Logger | ||
} | ||
|
||
func (s *statusServer) run(ctx context.Context) error { | ||
srv := &http.Server{ | ||
Addr: ":19180", | ||
Handler: http.HandlerFunc(s.serve), | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
srv.Close() | ||
}() | ||
|
||
s.logger.Debug("start http listener", "addr", srv.Addr) | ||
return srv.ListenAndServe() | ||
} | ||
|
||
func (s *statusServer) serve(w http.ResponseWriter, r *http.Request) { | ||
if err := s.serveErr(w, r); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprintf(w, "server error: %v", err.Error()) | ||
} | ||
} | ||
|
||
func (s *statusServer) serveErr(w http.ResponseWriter, _ *http.Request) error { | ||
clients, err := s.getClients() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
peers, err := s.getPeers() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
relays, err := s.getRelays() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w.Header().Add("Content-Type", "application/json") | ||
enc := json.NewEncoder(w) | ||
return enc.Encode(status{ | ||
ServerID: s.relays.id, | ||
Clients: clients, | ||
Peers: peers, | ||
Relays: relays, | ||
}) | ||
} | ||
|
||
func (s *statusServer) getClients() ([]statusClient, error) { | ||
clientMsgs, _, err := s.clients.conns.Snapshot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var clients []statusClient | ||
for _, msg := range clientMsgs { | ||
clients = append(clients, statusClient{ | ||
ID: msg.Key.ID, | ||
Addr: msg.Value.Addr, | ||
}) | ||
} | ||
|
||
return clients, nil | ||
} | ||
|
||
func (s *statusServer) getPeers() ([]statusPeer, error) { | ||
peerMsgs, _, err := s.clients.peers.Snapshot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var peers []statusPeer | ||
for _, msg := range peerMsgs { | ||
peers = append(peers, statusPeer{ | ||
ID: msg.Key.ID, | ||
Role: msg.Key.Role, | ||
Forward: msg.Key.Forward, | ||
}) | ||
} | ||
|
||
return peers, nil | ||
} | ||
|
||
func (s *statusServer) getRelays() ([]statusRelay, error) { | ||
msgs, _, err := s.relays.conns.Snapshot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var relays []statusRelay | ||
for _, msg := range msgs { | ||
relays = append(relays, statusRelay{ | ||
ID: msg.Key.ID, | ||
Hostport: msg.Value.Hostport.String(), | ||
}) | ||
} | ||
|
||
return relays, nil | ||
} | ||
|
||
type status struct { | ||
ServerID string `json:"server_id"` | ||
Clients []statusClient `json:"clients"` | ||
Peers []statusPeer `json:"peers"` | ||
Relays []statusRelay `json:"relays"` | ||
} | ||
|
||
type statusClient struct { | ||
ID ksuid.KSUID `json:"id"` | ||
Addr string `json:"addr"` | ||
} | ||
|
||
type statusPeer struct { | ||
ID ksuid.KSUID `json:"id"` | ||
Role model.Role `json:"role"` | ||
Forward model.Forward `json:"forward"` | ||
} | ||
|
||
type statusRelay struct { | ||
ID ksuid.KSUID `json:"id"` | ||
Hostport string `json:"hostport"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package relay | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
"maps" | ||
"net/http" | ||
"slices" | ||
|
||
"github.com/connet-dev/connet/model" | ||
) | ||
|
||
type statusServer struct { | ||
control *controlClient | ||
clients *clientsServer | ||
logger *slog.Logger | ||
} | ||
|
||
func (s *statusServer) run(ctx context.Context) error { | ||
srv := &http.Server{ | ||
Addr: ":19181", | ||
Handler: http.HandlerFunc(s.serve), | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
srv.Close() | ||
}() | ||
|
||
s.logger.Debug("start http listener", "addr", srv.Addr) | ||
return srv.ListenAndServe() | ||
} | ||
|
||
func (s *statusServer) serve(w http.ResponseWriter, r *http.Request) { | ||
if err := s.serveErr(w, r); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprintf(w, "server error: %v", err.Error()) | ||
} | ||
} | ||
|
||
func (s *statusServer) serveErr(w http.ResponseWriter, _ *http.Request) error { | ||
stat := "disconnected" | ||
if s.control.connStatus.Load() { | ||
stat = "online" | ||
} | ||
controlID, err := s.getControlID() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fwds := s.getForwards() | ||
|
||
w.Header().Add("Content-Type", "application/json") | ||
enc := json.NewEncoder(w) | ||
return enc.Encode(status{ | ||
Status: stat, | ||
Hostport: s.control.hostport.String(), | ||
ControlServerAddr: s.control.controlAddr.String(), | ||
ControlServerID: controlID, | ||
Forwards: fwds, | ||
}) | ||
} | ||
|
||
func (s *statusServer) getControlID() (string, error) { | ||
controlIDConfig, err := s.control.config.GetOrDefault(configControlID, ConfigValue{}) | ||
if err != nil { | ||
return "", err | ||
} | ||
return controlIDConfig.String, nil | ||
} | ||
|
||
func (s *statusServer) getForwards() []model.Forward { | ||
s.clients.forwardMu.RLock() | ||
defer s.clients.forwardMu.RUnlock() | ||
|
||
return slices.Collect(maps.Keys(s.clients.forwards)) | ||
} | ||
|
||
type status struct { | ||
Status string `json:"status"` | ||
Hostport string `json:"hostport"` | ||
ControlServerAddr string `json:"control_server_addr"` | ||
ControlServerID string `json:"control_server_id"` | ||
Forwards []model.Forward `json:"forwards"` | ||
} |