mirror of
https://github.com/sigcn/pg.git
synced 2026-04-23 00:37:30 +08:00
peermap: add admin api
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package disco
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/url"
|
||||
@@ -58,23 +55,6 @@ const (
|
||||
CONTROL_SERVER_CONNECTED ControlCode = 50
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func (e Error) Wrap(err error) Error {
|
||||
return Error{Code: e.Code, Msg: fmt.Sprintf("%s: %s", e.Msg, err)}
|
||||
}
|
||||
|
||||
func (e Error) Error() string {
|
||||
return fmt.Sprintf("ENO%d: %s", e.Code, e.Msg)
|
||||
}
|
||||
|
||||
func (e Error) MarshalTo(w io.Writer) {
|
||||
json.NewEncoder(w).Encode(e)
|
||||
}
|
||||
|
||||
type NATType string
|
||||
|
||||
func (t NATType) AccurateThan(t1 NATType) bool {
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package disco
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func NewNonce() string {
|
||||
buf := make([]byte, 1)
|
||||
n, _ := rand.Read(buf)
|
||||
if n != 1 {
|
||||
return "0"
|
||||
}
|
||||
if buf[0] == 0 {
|
||||
return NewNonce()
|
||||
}
|
||||
return fmt.Sprintf("%d", buf[0])
|
||||
}
|
||||
|
||||
func MustParseNonce(nonce string) byte {
|
||||
ret, err := strconv.ParseUint(nonce, 10, 8)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return byte(ret)
|
||||
}
|
||||
+4
-3
@@ -20,6 +20,7 @@ import (
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/sigcn/pg/disco"
|
||||
"github.com/sigcn/pg/langs"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
@@ -206,7 +207,7 @@ func (c *WSConn) dial(ctx context.Context, server string) error {
|
||||
handshake := http.Header{}
|
||||
handshake.Set("X-Network", networkSecret.Secret)
|
||||
handshake.Set("X-PeerID", c.peerID.String())
|
||||
handshake.Set("X-Nonce", disco.NewNonce())
|
||||
handshake.Set("X-Nonce", langs.NewNonce())
|
||||
handshake.Set("X-Metadata", c.metadata.Encode())
|
||||
if server == "" {
|
||||
server = c.server.URL
|
||||
@@ -229,7 +230,7 @@ func (c *WSConn) dial(ctx context.Context, server string) error {
|
||||
return fmt.Errorf("dial server %s: 404 not found", server)
|
||||
}
|
||||
if httpResp != nil && httpResp.StatusCode == http.StatusForbidden {
|
||||
var err disco.Error
|
||||
var err langs.Error
|
||||
json.NewDecoder(httpResp.Body).Decode(&err)
|
||||
defer httpResp.Body.Close()
|
||||
return err
|
||||
@@ -253,7 +254,7 @@ func (c *WSConn) dial(ctx context.Context, server string) error {
|
||||
}
|
||||
|
||||
c.rawConn.Store(conn)
|
||||
c.nonce = disco.MustParseNonce(httpResp.Header.Get("X-Nonce"))
|
||||
c.nonce = langs.MustParseNonce(httpResp.Header.Get("X-Nonce"))
|
||||
c.connectedServer = server
|
||||
c.activeTime.Store(time.Now().Unix())
|
||||
conn.SetPingHandler(func(appData string) error {
|
||||
|
||||
Reference in New Issue
Block a user