sing-box/experimental/libbox/remote_profile.go

42 lines
830 B
Go
Raw Normal View History

2023-07-26 04:36:51 +00:00
package libbox
import (
"net/url"
)
func GenerateRemoteProfileImportLink(name string, remoteURL string) string {
importLink := &url.URL{
Scheme: "sing-box",
Host: "import-remote-profile",
RawQuery: url.Values{"url": []string{remoteURL}}.Encode(),
Fragment: name,
}
return importLink.String()
}
type ImportRemoteProfile struct {
Name string
URL string
Host string
}
func ParseRemoteProfileImportLink(importLink string) (*ImportRemoteProfile, error) {
importURL, err := url.Parse(importLink)
if err != nil {
return nil, err
}
remoteURL, err := url.Parse(importURL.Query().Get("url"))
if err != nil {
return nil, err
}
name := importURL.Fragment
if name == "" {
name = remoteURL.Host
}
return &ImportRemoteProfile{
Name: name,
URL: remoteURL.String(),
Host: remoteURL.Host,
}, nil
}