remote_profile.go 830 B

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