fix api service test; add url api option

This commit is contained in:
Page Fault
2020-07-14 16:24:53 +00:00
parent 9dc8a7c391
commit 2147c561f1
3 changed files with 46 additions and 7 deletions
+16 -6
View File
@@ -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)
@@ -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{
@@ -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{
@@ -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)
}
}()
+29 -1
View File
@@ -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"`
@@ -46,6 +52,7 @@ type UrlConfig struct {
Shadowsocks `json:"shadowsocks"`
TLS `json:"ssl"`
Mux `json:"mux"`
API `json:"api"`
}
type url struct {
@@ -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 := ""
@@ -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)
}
@@ -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 {
+1
View File
@@ -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 {