Skip to content

Commit

Permalink
Helper func to parse ss URL
Browse files Browse the repository at this point in the history
  • Loading branch information
riobard committed Mar 13, 2017
1 parent e63edf0 commit c5ab4c6
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,13 @@ func main() {
addr := flags.Client
cipher := flags.Cipher
password := flags.Password
var err error

if strings.HasPrefix(addr, "ss://") {
u, err := url.Parse(addr)
addr, cipher, password, err = parseURL(addr)
if err != nil {
log.Fatal(err)
}

addr = u.Host
if u.User != nil {
cipher = u.User.Username()
password, _ = u.User.Password()
}
}

ciph, err := core.PickCipher(cipher, key, password)
Expand Down Expand Up @@ -134,18 +129,13 @@ func main() {
addr := flags.Server
cipher := flags.Cipher
password := flags.Password
var err error

if strings.HasPrefix(addr, "ss://") {
u, err := url.Parse(addr)
addr, cipher, password, err = parseURL(addr)
if err != nil {
log.Fatal(err)
}

addr = u.Host
if u.User != nil {
cipher = u.User.Username()
password, _ = u.User.Password()
}
}

ciph, err := core.PickCipher(cipher, key, password)
Expand All @@ -161,3 +151,17 @@ func main() {
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
}

func parseURL(s string) (addr, cipher, password string, err error) {
u, err := url.Parse(s)
if err != nil {
return
}

addr = u.Host
if u.User != nil {
cipher = u.User.Username()
password, _ = u.User.Password()
}
return
}

0 comments on commit c5ab4c6

Please sign in to comment.