Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #429 from XanthusL/master
Browse files Browse the repository at this point in the history
Feature add: URI for shadowsocks-local
  • Loading branch information
arthurkiller authored Aug 27, 2018
2 parents bb41440 + c5d5382 commit 4a3718f
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"bytes"
"encoding/base64"
"encoding/binary"
"errors"
"flag"
Expand All @@ -12,6 +14,7 @@ import (
"os"
"path"
"strconv"
"strings"
"time"

ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
Expand Down Expand Up @@ -347,10 +350,61 @@ func enoughOptions(config *ss.Config) bool {
config.LocalPort != 0 && config.Password != ""
}

func parseURI(u string, cfg *ss.Config) (string, error) {
if u == "" {
return "", nil
}
invalidURI := errors.New("invalid URI")
// ss://base64(method:password)@host:port
// ss://base64(method:password@host:port)
u = strings.TrimLeft(u, "ss://")
i := strings.IndexRune(u, '@')
var headParts, tailParts [][]byte
if i == -1 {
dat, err := base64.StdEncoding.DecodeString(u)
if err != nil {
return "", err
}
parts := bytes.Split(dat, []byte("@"))
if len(parts) != 2 {
return "", invalidURI
}
headParts = bytes.SplitN(parts[0], []byte(":"), 2)
tailParts = bytes.SplitN(parts[1], []byte(":"), 2)

} else {
if i+1 >= len(u) {
return "", invalidURI
}
tailParts = bytes.SplitN([]byte(u[i+1:]), []byte(":"), 2)
dat, err := base64.StdEncoding.DecodeString(u[:i])
if err != nil {
return "", err
}
headParts = bytes.SplitN(dat, []byte(":"), 2)
}
if len(headParts) != 2 {
return "", invalidURI
}

if len(tailParts) != 2 {
return "", invalidURI
}
cfg.Method = string(headParts[0])
cfg.Password = string(headParts[1])
p, e := strconv.Atoi(string(tailParts[1]))
if e != nil {
return "", e
}
cfg.ServerPort = p
return string(tailParts[0]), nil

}

func main() {
log.SetOutput(os.Stdout)

var configFile, cmdServer string
var configFile, cmdServer, cmdURI string
var cmdConfig ss.Config
var printVer bool

Expand All @@ -364,9 +418,18 @@ func main() {
flag.IntVar(&cmdConfig.LocalPort, "l", 0, "local socks5 proxy port")
flag.StringVar(&cmdConfig.Method, "m", "", "encryption method, default: aes-256-cfb")
flag.BoolVar((*bool)(&debug), "d", false, "print debug message")
flag.StringVar(&cmdURI, "u", "", "shadowsocks URI")

flag.Parse()

if s, e := parseURI(cmdURI, &cmdConfig); e != nil {
log.Printf("invalid URI: %s\n", e.Error())
flag.Usage()
os.Exit(1)
} else if s != "" {
cmdServer = s
}

if printVer {
ss.PrintVersion()
os.Exit(0)
Expand Down

0 comments on commit 4a3718f

Please sign in to comment.