Skip to content

Commit

Permalink
fix api service test; add url api option
Browse files Browse the repository at this point in the history
  • Loading branch information
p4gefau1t committed Jul 14, 2020
1 parent 9dc8a7c commit 2147c56
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
22 changes: 16 additions & 6 deletions api/service/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func TestServerAPI(t *testing.T) {
resp2, err := stream2.Recv()
common.Must(err)
if resp2.Status.TrafficTotal.DownloadTraffic != 1234 || resp2.Status.TrafficTotal.UploadTraffic != 5678 {
t.Fail()
t.Fatal("wrong traffic")
}
if resp2.Status.SpeedCurrent.DownloadSpeed != 1234 || resp2.Status.TrafficTotal.UploadTraffic != 5678 {
t.Fail()
t.Fatal("wrong speed")
}

stream3, err := server.SetUsers(ctx)
Expand All @@ -83,11 +83,11 @@ func TestServerAPI(t *testing.T) {
})
resp3, err := stream3.Recv()
if err != nil || !resp3.Success {
t.Fail()
t.Fatal("user not exists")
}
valid, _ := auth.AuthUser("hash1234")
if valid {
t.Fail()
t.Fatal("failed to auth")
}
stream3.Send(&SetUsersRequest{
Status: &UserStatus{
Expand All @@ -99,11 +99,11 @@ func TestServerAPI(t *testing.T) {
})
resp3, err = stream3.Recv()
if err != nil || !resp3.Success {
t.Fail()
t.Fatal("failed to read")
}
valid, user = auth.AuthUser("newhash")
if !valid {
t.Fail()
t.Fatal("failed to auth 2")
}
stream3.Send(&SetUsersRequest{
Status: &UserStatus{
Expand All @@ -123,11 +123,21 @@ func TestServerAPI(t *testing.T) {
})
go func() {
for {
select {
case <-ctx.Done():
return
default:
}
user.AddTraffic(200, 0)
}
}()
go func() {
for {
select {
case <-ctx.Done():
return
default:
}
user.AddTraffic(0, 300)
}
}()
Expand Down
30 changes: 29 additions & 1 deletion url/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ type Mux struct {
Enabled bool
}

type API struct {
Enabled bool `json:"enabled"`
APIHost string `json:"api_addr"`
APIPort int `json:"api_port"`
}

type UrlConfig struct {
RunType string `json:"run_type"`
LocalAddr string `json:"local_addr"`
Expand All @@ -46,6 +52,7 @@ type UrlConfig struct {
Shadowsocks `json:"shadowsocks"`
TLS `json:"ssl"`
Mux `json:"mux"`
API `json:"api"`
}

type url struct {
Expand Down Expand Up @@ -84,6 +91,11 @@ func (u *url) Handle() error {
muxEnabled := false
listenHost := "127.0.0.1"
listenPort := 1080

apiEnabled := false
apiHost := "127.0.0.1"
apiPort := 10000

options := strings.Split(*u.option, ";")
for _, o := range options {
key := ""
Expand All @@ -106,11 +118,22 @@ func (u *url) Handle() error {
log.Fatal(err)
}
listenHost = h
lp, err := strconv.ParseUint(p, 10, 16)
lp, err := strconv.Atoi(p)
if err != nil {
log.Fatal(err)
}
listenPort = int(lp)
case "api":
h, p, err := net.SplitHostPort(val)
if err != nil {
log.Fatal(err)
}
apiHost = h
lp, err := strconv.Atoi(p)
if err != nil {
log.Fatal(err)
}
apiPort = int(lp)
default:
log.Fatal("invalid option", o)
}
Expand Down Expand Up @@ -138,6 +161,11 @@ func (u *url) Handle() error {
Password: ssPassword,
Method: ssMethod,
},
API: API{
Enabled: apiEnabled,
APIHost: apiHost,
APIPort: apiPort,
},
}
data, err := json.Marshal(&config)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions url/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestUrl_Handle(t *testing.T) {
optionCases := []string{
"mux=true;listen=127.0.0.1:0",
"mux=false;listen=127.0.0.1:0",
"mux=false;listen=127.0.0.1:0;api=127.0.0.1:0",
}
for _, s := range urlCases {
for _, option := range optionCases {
Expand Down

0 comments on commit 2147c56

Please sign in to comment.