Skip to content

Commit

Permalink
remove duplicate URL parse
Browse files Browse the repository at this point in the history
  • Loading branch information
pmoieni committed Aug 25, 2024
1 parent a4a5683 commit 7c8ae50
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions x/examples/outline-cli/outline_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ type OutlineDevice struct {
var configToDialer = config.NewDefaultConfigToDialer()

func NewOutlineDevice(transportConfig string) (od *OutlineDevice, err error) {
transportConfig, err = formatConfig(transportConfig)
if err := validateConfig(transportConfig); err != nil {
return nil, err
}
parsed, err := url.Parse(transportConfig)
if err != nil {
return nil, fmt.Errorf("failed to parse config URL: %w", err)
}
transportConfig, err = formatConfig(parsed)
if err != nil {
return nil, err
}
ip, err := resolveShadowsocksServerIPFromConfig(transportConfig)
ip, err := resolveShadowsocksServerIPFromHostname(parsed.Hostname())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -107,13 +114,9 @@ func constructShadowsocksSessionConfig(resp []byte) (string, error) {
}

func fetchShadowsocksSessionConfig(transportConfig string) ([]byte, error) {
parsed, err := url.Parse(transportConfig)
if err != nil {
return nil, fmt.Errorf("failed to parse config URL: %w", err)
}
parsed.Scheme = "https"
transportConfig = "https" + strings.TrimPrefix(transportConfig, "ssconf")

resp, err := http.Get(parsed.String())
resp, err := http.Get(transportConfig)
if err != nil {
return nil, fmt.Errorf("failed to fetch config from ssconf: %w", err)
}
Expand All @@ -126,23 +129,12 @@ func fetchShadowsocksSessionConfig(transportConfig string) ([]byte, error) {
return body, nil
}

func formatConfig(transportConfig string) (string, error) {
if strings.Contains(transportConfig, "|") {
return "", errors.New("multi-part config is not supported")
}
if transportConfig = strings.TrimSpace(transportConfig); transportConfig == "" {
return "", errors.New("config is required")
}
parsed, err := url.Parse(transportConfig)
if err != nil {
return "", fmt.Errorf("failed to parse config URL: %w", err)
}

switch parsed.Scheme {
func formatConfig(transportConfigURL *url.URL) (string, error) {
switch transportConfigURL.Scheme {
case "ss":
return transportConfig, nil
return transportConfigURL.String(), nil
case "ssconf":
fetched, err := fetchShadowsocksSessionConfig(transportConfig)
fetched, err := fetchShadowsocksSessionConfig(transportConfigURL.String())
if err != nil {
return "", err
}
Expand All @@ -152,13 +144,18 @@ func formatConfig(transportConfig string) (string, error) {
}
}

func resolveShadowsocksServerIPFromConfig(transportConfig string) (net.IP, error) {
parsed, err := url.Parse(transportConfig)
if err != nil {
return nil, fmt.Errorf("failed to parse config URL: %w", err)
func validateConfig(transportConfig string) error {
if strings.Contains(transportConfig, "|") {
return errors.New("multi-part config is not supported")
}
if transportConfig = strings.TrimSpace(transportConfig); transportConfig == "" {
return errors.New("config is required")
}
return nil
}

ipList, err := net.LookupIP(parsed.Hostname())
func resolveShadowsocksServerIPFromHostname(hostname string) (net.IP, error) {
ipList, err := net.LookupIP(hostname)
if err != nil {
return nil, fmt.Errorf("invalid server hostname: %w", err)
}
Expand Down

0 comments on commit 7c8ae50

Please sign in to comment.