| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/sylvinus/agent-vm/internal/managedproxy" |
| 9 | ) |
| 10 | |
| 11 | func TestFailedSubscriptionRefreshKeepsWorkingNodesAndSecretsPrivate(t *testing.T) { |
| 12 | s := testServer(t) |
| 13 | listener, err := net.Listen("tcp4", "127.0.0.1:0") |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | defer listener.Close() |
| 18 | go func() { |
| 19 | for { |
| 20 | conn, e := listener.Accept() |
| 21 | if e != nil { |
| 22 | return |
| 23 | } |
| 24 | conn.Close() |
| 25 | } |
| 26 | }() |
| 27 | cfg, err := loadConfig(s.configPath) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | p, _ := managedproxy.Load(s.profile) |
| 32 | p.Port = listener.Addr().(*net.TCPAddr).Port |
| 33 | cfg.ProxyURL = p.Endpoint() |
| 34 | if err = writePrivate(s.configPath, cfg); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if err = p.Import("source", "https://example.com/sub?token=private-subscription", []byte(`proxies: [{name: qa, type: socks5, server: 127.0.0.1, port: 1080, password: private-node-password}]`), "source", 24, ""); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | p.Selected = p.Sources[0].Nodes[0].ID |
| 41 | if err = p.Save(s.profile); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | before := p.Sources[0] |
| 45 | if err = s.changeProxy("refresh", proxyInput{ID: "source"}, nil); err == nil { |
| 46 | t.Fatal("broken subscription accepted") |
| 47 | } |
| 48 | after, err := managedproxy.Load(s.profile) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if after.Sources[0].Nodes[0].ID != before.Nodes[0].ID || after.Selected != p.Selected || !after.Sources[0].Updated.Equal(before.Updated) || after.Sources[0].Error == "" { |
| 53 | t.Fatal("failed refresh replaced last good data") |
| 54 | } |
| 55 | w := request(s, "GET", "/api/proxy", s.origin, "", true) |
| 56 | if w.Code != 200 { |
| 57 | t.Fatal(w.Code, w.Body.String()) |
| 58 | } |
| 59 | for _, secret := range []string{"private-subscription", "private-node-password", p.Auth} { |
| 60 | if strings.Contains(w.Body.String(), secret) { |
| 61 | t.Fatal("status leaked proxy credentials") |
| 62 | } |
| 63 | } |
| 64 | if w = request(s, "POST", "/api/proxy/stop", s.origin, `{}`, false); w.Code != 401 { |
| 65 | t.Fatal("proxy mutation accepted without authentication") |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestSettingsCannotWriteProxyEndpoint(t *testing.T) { |
| 70 | s := testServer(t) |
| 71 | for _, body := range []string{`{"proxy_url":"socks5://127.0.0.1:7890"}`, `{"proxy_username":"user"}`, `{"proxy_password":"secret"}`} { |
| 72 | if w := request(s, "POST", "/api/settings", s.origin, body, true); w.Code != 400 { |
| 73 | t.Fatal("removed proxy settings accepted", w.Code) |
| 74 | } |
| 75 | } |
| 76 | if w := request(s, "POST", "/api/proxy/migrate", s.origin, `{}`, true); w.Code != 404 { |
| 77 | t.Fatal("removed migration entry accepted", w.Code) |
| 78 | } |
| 79 | } |