2024-01-10 14:19:21 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/build"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"go.sia.tech/jape"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"net/url"
|
|
|
|
"nhooyr.io/websocket"
|
|
|
|
)
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
var _ Service = (*HTTPService)(nil)
|
2024-01-10 14:19:21 +00:00
|
|
|
|
2024-01-17 20:51:41 +00:00
|
|
|
type P2PNodesResponse struct {
|
|
|
|
Nodes []P2PNodeResponse `json:"nodes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type P2PNodeResponse struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Uris []string `json:"uris"`
|
|
|
|
}
|
|
|
|
|
2024-01-30 01:52:17 +00:00
|
|
|
type HTTPServiceInterface interface {
|
|
|
|
GetHttpRouter(inject map[string]jape.Handler) *httprouter.Router
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
type HTTPService struct {
|
2024-01-29 06:10:04 +00:00
|
|
|
ServiceBase
|
2024-01-15 03:06:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 06:10:04 +00:00
|
|
|
func NewHTTP(params ServiceParams) *HTTPService {
|
2024-01-29 04:59:43 +00:00
|
|
|
return &HTTPService{
|
2024-01-29 06:10:04 +00:00
|
|
|
ServiceBase: ServiceBase{
|
|
|
|
logger: params.Logger,
|
|
|
|
config: params.Config,
|
|
|
|
db: params.Db,
|
|
|
|
},
|
2024-01-15 01:53:44 +00:00
|
|
|
}
|
2024-01-10 14:19:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (h *HTTPService) GetHttpRouter(inject map[string]jape.Handler) *httprouter.Router {
|
2024-01-16 16:26:27 +00:00
|
|
|
routes := map[string]jape.Handler{
|
2024-01-17 20:51:41 +00:00
|
|
|
"GET /s5/version": h.versionHandler,
|
|
|
|
"GET /s5/p2p": h.p2pHandler,
|
2024-01-17 22:02:39 +00:00
|
|
|
"GET /s5/p2p/nodes": h.p2pNodesHandler,
|
2024-01-16 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range inject {
|
|
|
|
routes[k] = v
|
|
|
|
}
|
2024-01-10 14:19:21 +00:00
|
|
|
|
2024-01-16 16:26:27 +00:00
|
|
|
return jape.Mux(routes)
|
2024-01-10 14:19:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (h *HTTPService) Start() error {
|
2024-01-10 14:19:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (h *HTTPService) Stop() error {
|
2024-01-10 14:19:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (h *HTTPService) Init() error {
|
2024-01-10 14:19:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (h *HTTPService) versionHandler(ctx jape.Context) {
|
2024-01-10 14:19:21 +00:00
|
|
|
_, _ = ctx.ResponseWriter.Write([]byte(build.Version))
|
|
|
|
}
|
2024-01-29 04:59:43 +00:00
|
|
|
func (h *HTTPService) p2pHandler(ctx jape.Context) {
|
2024-01-10 14:19:21 +00:00
|
|
|
c, err := websocket.Accept(ctx.ResponseWriter, ctx.Request, nil)
|
|
|
|
if err != nil {
|
2024-01-29 06:10:04 +00:00
|
|
|
h.logger.Error("error accepting websocket connection", zap.Error(err))
|
2024-01-10 14:19:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := net.CreateTransportPeer("wss", &net.TransportPeerConfig{
|
|
|
|
Socket: c,
|
|
|
|
Uris: []*url.URL{},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-29 06:10:04 +00:00
|
|
|
h.logger.Error("error creating transport peer", zap.Error(err))
|
2024-01-10 14:19:21 +00:00
|
|
|
err := c.Close(websocket.StatusInternalError, "the sky is falling")
|
|
|
|
if err != nil {
|
2024-01-29 06:10:04 +00:00
|
|
|
h.logger.Error("error closing websocket connection", zap.Error(err))
|
2024-01-10 14:19:21 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-01-10 16:21:07 +00:00
|
|
|
|
2024-01-29 06:10:04 +00:00
|
|
|
h.services.P2P().ConnectionTracker().Add(1)
|
2024-01-10 16:21:07 +00:00
|
|
|
|
2024-01-10 14:19:21 +00:00
|
|
|
go func() {
|
2024-01-29 06:10:04 +00:00
|
|
|
err := h.services.P2P().OnNewPeer(peer, false)
|
2024-01-10 14:19:21 +00:00
|
|
|
if err != nil {
|
2024-01-29 06:10:04 +00:00
|
|
|
h.logger.Error("error handling new peer", zap.Error(err))
|
2024-01-10 14:19:21 +00:00
|
|
|
}
|
2024-01-29 06:10:04 +00:00
|
|
|
h.services.P2P().ConnectionTracker().Done()
|
2024-01-10 14:19:21 +00:00
|
|
|
}()
|
|
|
|
}
|
2024-01-17 20:51:41 +00:00
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (h *HTTPService) p2pNodesHandler(ctx jape.Context) {
|
2024-01-29 06:10:04 +00:00
|
|
|
localId, err := h.services.P2P().NodeId().ToString()
|
2024-01-17 20:51:41 +00:00
|
|
|
|
|
|
|
if ctx.Check("error getting local node id", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-29 06:10:04 +00:00
|
|
|
uris := h.services.P2P().SelfConnectionUris()
|
2024-01-17 20:51:41 +00:00
|
|
|
|
|
|
|
nodeList := make([]P2PNodeResponse, len(uris))
|
|
|
|
|
|
|
|
for i, uri := range uris {
|
|
|
|
nodeList[i] = P2PNodeResponse{
|
|
|
|
Id: localId,
|
|
|
|
Uris: []string{uri.String()},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Encode(P2PNodesResponse{
|
|
|
|
Nodes: nodeList,
|
|
|
|
})
|
|
|
|
}
|