实现手动 proxy 同步接口
This commit is contained in:
@@ -19,7 +19,11 @@ func IsGostNotFound(err error) bool {
|
||||
}
|
||||
|
||||
type GostClient interface {
|
||||
ListChains() ([]*GostChainConfig, error)
|
||||
GetChain(name string) (*GostChainConfig, error)
|
||||
CreateChain(chain *GostChainConfig) error
|
||||
DeleteChain(name string) error
|
||||
SaveConfig() error
|
||||
CreateService(service *GostServiceConfig) error
|
||||
DeleteService(name string) error
|
||||
CreateAuther(auther *GostAutherConfig) error
|
||||
@@ -54,15 +58,37 @@ func NewGost(host string, port int, pathPrefix, username, password string) GostC
|
||||
}
|
||||
|
||||
type GostChainConfig struct {
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name"`
|
||||
Hops []GostHopConfig `json:"hops,omitempty"`
|
||||
}
|
||||
|
||||
type GostHopConfig struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Nodes []GostNodeConfig `json:"nodes,omitempty"`
|
||||
}
|
||||
|
||||
type GostNodeConfig struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Addr string `json:"addr"`
|
||||
Connector GostConnectorConfig `json:"connector"`
|
||||
Dialer GostDialerConfig `json:"dialer"`
|
||||
}
|
||||
|
||||
type GostConnectorConfig struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type GostDialerConfig struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type GostServiceConfig struct {
|
||||
Name string `json:"name"`
|
||||
Addr string `json:"addr"`
|
||||
Admission string `json:"admission,omitempty"`
|
||||
Handler GostHandlerConfig `json:"handler"`
|
||||
Listener GostListenerConfig `json:"listener"`
|
||||
Name string `json:"name"`
|
||||
Addr string `json:"addr"`
|
||||
Admission string `json:"admission,omitempty"`
|
||||
Handler GostHandlerConfig `json:"handler"`
|
||||
Listener GostListenerConfig `json:"listener"`
|
||||
Recorders []GostRecorderConfig `json:"recorders,omitempty"`
|
||||
}
|
||||
|
||||
type GostHandlerConfig struct {
|
||||
@@ -75,6 +101,11 @@ type GostListenerConfig struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type GostRecorderConfig struct {
|
||||
Name string `json:"name"`
|
||||
Record string `json:"record"`
|
||||
}
|
||||
|
||||
type GostAutherConfig struct {
|
||||
Name string `json:"name"`
|
||||
Auths []GostAuthConfig `json:"auths"`
|
||||
@@ -115,6 +146,40 @@ func (c *gostClient) GetChain(name string) (*GostChainConfig, error) {
|
||||
return &GostChainConfig{Name: name}, nil
|
||||
}
|
||||
|
||||
func (c *gostClient) ListChains() ([]*GostChainConfig, error) {
|
||||
body, err := c.get("/config/chains")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(body) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
Data struct {
|
||||
Count int `json:"count"`
|
||||
List []*GostChainConfig `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &resp); err != nil {
|
||||
return nil, fmt.Errorf("parse gost chain list failed %s: %w", string(body), err)
|
||||
}
|
||||
|
||||
return resp.Data.List, nil
|
||||
}
|
||||
|
||||
func (c *gostClient) CreateChain(chain *GostChainConfig) error {
|
||||
return c.create("/config/chains", chain)
|
||||
}
|
||||
|
||||
func (c *gostClient) DeleteChain(name string) error {
|
||||
return c.delete("/config/chains/" + url.PathEscape(name))
|
||||
}
|
||||
|
||||
func (c *gostClient) SaveConfig() error {
|
||||
return c.create("/config", nil)
|
||||
}
|
||||
|
||||
func (c *gostClient) CreateService(service *GostServiceConfig) error {
|
||||
return c.create("/config/services", service)
|
||||
}
|
||||
|
||||
107
web/globals/gost_test.go
Normal file
107
web/globals/gost_test.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package globals
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGostClientChainOperations(t *testing.T) {
|
||||
var (
|
||||
created *GostChainConfig
|
||||
deleted []string
|
||||
saved bool
|
||||
)
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || username != "user" || password != "pass" {
|
||||
t.Errorf("unexpected auth: ok=%v username=%q password=%q", ok, username, password)
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
switch {
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/config/chains":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"count": 2,
|
||||
"list": []map[string]any{
|
||||
{"name": "old-a"},
|
||||
{"name": "old-b"},
|
||||
},
|
||||
})
|
||||
case r.Method == http.MethodPost && r.URL.Path == "/api/config/chains":
|
||||
if err := json.NewDecoder(r.Body).Decode(&created); err != nil {
|
||||
t.Errorf("Decode chain failed: %v", err)
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
case r.Method == http.MethodDelete && r.URL.Path == "/api/config/chains/old-a":
|
||||
deleted = append(deleted, "old-a")
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
case r.Method == http.MethodDelete && r.URL.Path == "/api/config/chains/old-b":
|
||||
deleted = append(deleted, "old-b")
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
case r.Method == http.MethodPost && r.URL.Path == "/api/config":
|
||||
saved = true
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
default:
|
||||
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewGost(server.URL, 9700, "/api", "user", "pass")
|
||||
|
||||
chains, err := client.ListChains()
|
||||
if err != nil {
|
||||
t.Fatalf("ListChains returned error: %v", err)
|
||||
}
|
||||
if len(chains) != 2 || chains[0].Name != "old-a" || chains[1].Name != "old-b" {
|
||||
t.Fatalf("unexpected chains: %#v", chains)
|
||||
}
|
||||
|
||||
if err := client.DeleteChain(chains[0].Name); err != nil {
|
||||
t.Fatalf("DeleteChain old-a returned error: %v", err)
|
||||
}
|
||||
if err := client.DeleteChain(chains[1].Name); err != nil {
|
||||
t.Fatalf("DeleteChain old-b returned error: %v", err)
|
||||
}
|
||||
if len(deleted) != 2 {
|
||||
t.Fatalf("unexpected deleted chains: %#v", deleted)
|
||||
}
|
||||
|
||||
err = client.CreateChain(&GostChainConfig{
|
||||
Name: "edge-a",
|
||||
Hops: []GostHopConfig{{
|
||||
Nodes: []GostNodeConfig{{
|
||||
Addr: "192.0.2.1:1080",
|
||||
Connector: GostConnectorConfig{Type: "socks5"},
|
||||
Dialer: GostDialerConfig{Type: "tcp"},
|
||||
}},
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateChain returned error: %v", err)
|
||||
}
|
||||
if created == nil || created.Name != "edge-a" {
|
||||
t.Fatalf("unexpected created chain: %#v", created)
|
||||
}
|
||||
if len(created.Hops) != 1 || len(created.Hops[0].Nodes) != 1 {
|
||||
t.Fatalf("unexpected created chain hops: %#v", created.Hops)
|
||||
}
|
||||
node := created.Hops[0].Nodes[0]
|
||||
if node.Addr != "192.0.2.1:1080" || node.Connector.Type != "socks5" || node.Dialer.Type != "tcp" {
|
||||
t.Fatalf("unexpected created node: %#v", node)
|
||||
}
|
||||
|
||||
if err := client.SaveConfig(); err != nil {
|
||||
t.Fatalf("SaveConfig returned error: %v", err)
|
||||
}
|
||||
if !saved {
|
||||
t.Fatal("expected SaveConfig request")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user