Skip to content

Commit d07c835

Browse files
authored
merge: PR #3 from feature/jsonsub-support
feature/jsonsub-support
2 parents b9f20cd + 9576c9b commit d07c835

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ require (
3939
github.com/go-playground/validator/v10 v10.28.0 // indirect
4040
github.com/goccy/go-json v0.10.5 // indirect
4141
github.com/goccy/go-yaml v1.18.0 // indirect
42-
github.com/gogf/gf/v2 v2.9.5 // indirect
42+
github.com/gogf/gf/v2 v2.9.5
4343
github.com/google/btree v1.1.2 // indirect
4444
github.com/gorilla/websocket v1.5.3 // indirect
4545
github.com/gvcgo/vpnparser v0.2.7

internal/api/v1/probe/handler.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,17 @@ func (h *ProbeHandler) parseProbeParams(ctx *gin.Context) (out ProbeParams, ok b
7878
}
7979

8080
func (h *ProbeHandler) parseXrayConf(ctx *gin.Context, params *ProbeParams) (out *core.Config, ok bool) {
81-
config, err := serial.ParseURI(serial.CONFIG_BACKEND_XRAYCORE, params.Connection, &serial.ParseParams{EnableDebug: true})
81+
var config string
82+
var err error
83+
switch params.Schema {
84+
case "http://", "https://":
85+
log.Println("[DEBUG] probe/handler: assuming that ctx is json subscription link")
86+
config, err = serial.ParseSubscriptionURI(params.Connection, &serial.ParseSubParams{EnableDebug: true})
87+
default:
88+
log.Println("[DEBUG] probe/handler: assuming that ctx is direct vpn connection uri")
89+
config, err = serial.ParseURI(serial.CONFIG_BACKEND_XRAYCORE, params.Connection, &serial.ParseParams{EnableDebug: true})
90+
}
91+
8292
if err != nil {
8393
ctx.String(http.StatusBadRequest, "Unable to parse uri-based config for xray-core due: %v", err)
8494
return out, false

internal/serial/parse.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package serial
22

33
import (
4+
"cmp"
5+
"fmt"
6+
"io"
47
"log"
8+
"net/http"
59
"net/url"
10+
"time"
611

12+
"github.com/gogf/gf/v2/encoding/gjson"
713
"github.com/quyxishi/whitebox/internal/serial/xray"
814

915
_ "github.com/xtls/xray-core/main/json"
@@ -50,3 +56,52 @@ func ParseURI(backend BackendType, uri string, params *ParseParams) (out string,
5056

5157
return out, nil
5258
}
59+
60+
type ParseSubParams struct {
61+
EnableDebug bool // {"loglevel":"debug","access":"none","error":""}
62+
FetchTimeout time.Duration
63+
}
64+
65+
func ParseSubscriptionURI(json_sub_uri string, params *ParseSubParams) (out string, err error) {
66+
cli := http.Client{Timeout: cmp.Or(params.FetchTimeout, 5*time.Second)}
67+
resp, err := cli.Get(json_sub_uri)
68+
if err != nil {
69+
return "", fmt.Errorf("failed to fetch json subscription uri: %v", err)
70+
}
71+
if resp != nil {
72+
defer func() {
73+
if err := resp.Body.Close(); err != nil {
74+
log.Printf("[ERROR] serial/parse: failed to close response.body instance: %v", err)
75+
}
76+
}()
77+
}
78+
79+
outRaw, err := io.ReadAll(resp.Body)
80+
if err != nil {
81+
return "", fmt.Errorf("failed to read body from json subscription uri: %v", err)
82+
}
83+
84+
if params.EnableDebug {
85+
debugFields := []struct {
86+
key string
87+
val any
88+
}{
89+
{"log.loglevel", "debug"},
90+
{"log.access", "none"},
91+
{"log.error", ""},
92+
}
93+
94+
j := gjson.New(outRaw)
95+
96+
for _, s := range debugFields {
97+
if err := j.Set(s.key, s.val); err != nil {
98+
return "", fmt.Errorf("failed to patch config with key '%s': %w", s.key, err)
99+
}
100+
}
101+
102+
outRaw = []byte(j.MustToJsonString())
103+
}
104+
105+
out = string(outRaw)
106+
return out, nil
107+
}

internal/serial/parse_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const (
3838
URI_SHADOWSOCKS_XHTTP_TLS string = "ss://MjAyMi1ibGFrZTMtY2hhY2hhMjAtcG9seTEzMDU6SFJvWEFQRWh1M1BLQWk1bCtvWnR0MmxReDA3NUVVUktRWnpLa1BVeWlqWT0@1.2.3.4:443?type=xhttp&path=%2Fp&host=h&mode=auto&security=tls&fp=chrome&alpn=h2%2Chttp%2F1.1&ech=AF3%2BDQBZAAAgACD21HO7PNeZMft4mWQiZguw0MzGRrK2oNZjXe8gbTbfGQAkAAEAAQABAAIAAQADAAIAAQACAAIAAgADAAMAAQADAAIAAwADAApnb29nbGUuY29tAAA%3D&sni=google.com"
3939

4040
URI_WIREGUARD string = "wireguard://W0ludGVyZmFjZV0KUHJpdmF0ZUtleSA9IFNObk5ON0l4YzN0emxYS2FJNGY4NnEyOFYzbnhGS2YxcmNoYWt4bWdBbHM9CkFkZHJlc3MgPSAxMC4wLjAuMi8zMgpETlMgPSAxLjEuMS4xLCAxLjAuMC4xCk1UVSA9IDE0MjAKCiMgLTEKW1BlZXJdClB1YmxpY0tleSA9IHk2MTdkQ2dNM1g2bEtEanBkdDVhR2NBWmROWW5OT0FwMFMyanFUbGpmZzA9CkFsbG93ZWRJUHMgPSAwLjAuMC4wLzAsIDo6LzAKRW5kcG9pbnQgPSAxLjIuMy40OjI3Nzg5"
41+
42+
// URI_SUBJSON_VLESS string = "http://1.2.3.4:2096/json/l08vryrtn0gb07s4"
4143
)
4244

4345
func xrayParseAndLoad(t *testing.T, uri string) {
@@ -167,3 +169,23 @@ func TestParseURI_ShadowsocksXhttpTls(t *testing.T) {
167169
func TestParseURI_Wireguard(t *testing.T) {
168170
xrayParseAndLoad(t, URI_WIREGUARD)
169171
}
172+
173+
// -- SUBJSON
174+
175+
// func TestParseSubscriptionURI_Vless(t *testing.T) {
176+
// cfg, err := serial.ParseSubscriptionURI(URI_SUBJSON_VLESS, &serial.ParseSubParams{EnableDebug: true})
177+
// if err != nil {
178+
// t.Errorf("error occurred during parsing: %v", err)
179+
// return
180+
// }
181+
182+
// t.Log(cfg)
183+
184+
// _, err = core.LoadConfig("json", bytes.NewReader([]byte(cfg)))
185+
// if err != nil {
186+
// t.Errorf("unable to load xray config: %s", err.Error())
187+
// return
188+
// }
189+
190+
// t.Log("successfully loaded uri-based parsed config into xray-core")
191+
// }

0 commit comments

Comments
 (0)