github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/experimental/libbox/remote_profile.go (about) 1 package libbox 2 3 import ( 4 "net/url" 5 ) 6 7 func GenerateRemoteProfileImportLink(name string, remoteURL string) string { 8 importLink := &url.URL{ 9 Scheme: "sing-box", 10 Host: "import-remote-profile", 11 RawQuery: url.Values{"url": []string{remoteURL}}.Encode(), 12 Fragment: name, 13 } 14 return importLink.String() 15 } 16 17 type ImportRemoteProfile struct { 18 Name string 19 URL string 20 Host string 21 } 22 23 func ParseRemoteProfileImportLink(importLink string) (*ImportRemoteProfile, error) { 24 importURL, err := url.Parse(importLink) 25 if err != nil { 26 return nil, err 27 } 28 remoteURL, err := url.Parse(importURL.Query().Get("url")) 29 if err != nil { 30 return nil, err 31 } 32 name := importURL.Fragment 33 if name == "" { 34 name = remoteURL.Host 35 } 36 return &ImportRemoteProfile{ 37 Name: name, 38 URL: remoteURL.String(), 39 Host: remoteURL.Host, 40 }, nil 41 }