11package http
22
33import (
4- "fmt"
5-
64 "encoding/json"
5+ "fmt"
76 "github.com/YangYongZhi/muxy/log"
7+ "github.com/YangYongZhi/muxy/middleware"
88 m "github.com/YangYongZhi/muxy/run"
9+ "github.com/YangYongZhi/muxy/symptom"
910 "github.com/YangYongZhi/muxy/throttler"
1011 "io/ioutil"
1112 "net/http"
13+ "os/exec"
1214 "reflect"
1315 "time"
14-
15- "github.com/YangYongZhi/muxy/middleware"
16- "github.com/YangYongZhi/muxy/symptom"
1716)
1817
1918const (
@@ -33,19 +32,58 @@ func New(name string) *MuxyApiServer {
3332}
3433
3534func apiHandler (w http.ResponseWriter , r * http.Request ) {
36- log .Debug ("request the %s api " , log .Colorize (log .YELLOW , r .URL .Path [1 :]))
35+ log .Debug ("Request method : [%s] " , log .Colorize (log .YELLOW , r .URL .Path [1 :]))
3736 method := r .URL .Path [1 :]
38- fmt .Fprintf (w , "Hi, your resource is %s\n " , method )
37+ // fmt.Fprintf(w, "Hi, your resource is %s\n", method)
3938
4039 switch method {
41- case "check " :
40+ case "middlewares " :
4241 middlewares := Muxy .MiddleWares ()
4342 if len (middlewares ) > 0 {
44- fmt . Fprintf ( w , "Muxy has been start , middleware count : %d\n " , len (middlewares ))
43+ log . Debug ( "Muxy is running , middleware count : %d\n " , len (middlewares ))
4544 }
4645
47- //e.g. body = {"Device":"ens33","Latency": 2000, "TargetBandWidth":20,"PacketLoss":70,"TargetPorts": ["5001","10090"], "TargetProtos":["tcp","icmp"]}
46+ //middlewareJson, _ := json.Marshal(middlewares)
47+ //middlewareJsonString := string(middlewareJson)
48+
49+ middlewareJson , _ := json .MarshalIndent (middlewares , "" , " " )
50+ middlewareJsonString := string (middlewareJson )
51+ log .Debug ("middlewares are :\n %s" , middlewareJsonString )
52+
53+ w .Header ().Add ("Content-Type" , "application/json" )
54+ fmt .Fprintf (w , middlewareJsonString )
55+ case "networkshape" :
56+ // List all iptables
57+ iptCmdStr := fmt .Sprintf (throttler .IptList , throttler .Ip4Tables )
58+ iptCmd := exec .Command ("/bin/bash" , "-c" , iptCmdStr )
59+ iptOut , err := iptCmd .Output ()
60+ log .Debug ("Executed command : %s" , log .Colorize (log .GREEN , iptCmdStr ))
61+ if err != nil {
62+ log .Error ("Error: %s" , err .Error ())
63+ }
64+ fmt .Fprintf (w , "### %s ###:\n %s" , iptCmdStr , string (iptOut ))
65+
66+ // List all iptables 6
67+ ipt6CmdStr := fmt .Sprintf (throttler .IptList , throttler .Ip6Tables )
68+ ipt6Cmd := exec .Command ("/bin/bash" , "-c" , ipt6CmdStr )
69+ ipt6Out , err := ipt6Cmd .Output ()
70+ log .Debug ("Executed command : %s" , log .Colorize (log .GREEN , ipt6CmdStr ))
71+ if err != nil {
72+ log .Error ("Error: %s" , err .Error ())
73+ }
74+ fmt .Fprintf (w , "### %s ###:\n %s" , ipt6CmdStr , string (ipt6Out ))
75+
76+ // Show tc qdisc
77+ tcListCmd := exec .Command ("/bin/bash" , "-c" , throttler .TcList )
78+ tcOut , err := tcListCmd .Output ()
79+ log .Debug ("Executed command : %s" , log .Colorize (log .GREEN , throttler .TcList ))
80+ if err != nil {
81+ log .Error ("Error: %s" , err .Error ())
82+ }
83+ fmt .Fprintf (w , "### %s ###:\n %s" , throttler .TcList , string (tcOut ))
84+
4885 case "reset" :
86+ //e.g. body = {"Device":"ens33","Latency": 2000, "TargetBandWidth":20,"PacketLoss":70,"TargetPorts": ["5001","10090"], "TargetProtos":["tcp","icmp"]}
4987 log .Info ("Reset the middlewares, please wait." )
5088
5189 //decode := json.NewDecoder(r.Body)
@@ -58,10 +96,10 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
5896 var config throttler.Config
5997
6098 if err := json .Unmarshal (body , & config ); err == nil {
61- log .Debug ("latency :%s " , config .Latency )
62- fmt .Fprint ( w , body_str )
99+ log .Debug ("latency :%d " , config .Latency )
100+ fmt .Fprintf ( w , "%s \n " , body_str )
63101 } else {
64- log .Error ("Reset a network shape has an erro " , err )
102+ log .Error ("Reset a network shape has an error " , err )
65103 fmt .Fprint (w , err )
66104 }
67105
@@ -72,9 +110,9 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
72110
73111 switch v := m .(type ) {
74112 case * middleware.LoggerMiddleware :
75- log .Debug ("Not support %v now ." , v )
113+ log .Debug ("Not support %v yet ." , v )
76114 case * symptom.HTTPDelaySymptom :
77- log .Debug ("Not support %v now ." , v )
115+ log .Debug ("Not support %v yet ." , v )
78116 case * symptom.NetworkShaperSymptom :
79117 v .Teardown ()
80118
@@ -88,13 +126,25 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
88126 v .TargetBandwidth = config .TargetBandwidth
89127 v .TargetPorts = config .TargetPorts
90128 v .TargetProtos = config .TargetProtos
129+ if len (config .TargetIps ) > 0 {
130+ v .TargetIps = config .TargetIps
131+ }
132+ if len (config .TargetIps6 ) > 0 {
133+ v .TargetIps6 = config .TargetIps6
134+ }
91135
92136 v .Config .Device = config .Device
93137 v .Config .Latency = config .Latency
94138 v .Config .PacketLoss = config .PacketLoss
95139 v .Config .TargetBandwidth = config .TargetBandwidth
96140 v .Config .TargetPorts = config .TargetPorts
97141 v .Config .TargetProtos = config .TargetProtos
142+ if len (config .TargetIps ) > 0 {
143+ v .Config .TargetIps = config .TargetIps
144+ }
145+ if len (config .TargetIps6 ) > 0 {
146+ v .Config .TargetIps6 = config .TargetIps6
147+ }
98148
99149 v .Setup ()
100150 }
@@ -106,21 +156,27 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
106156 case "enable" :
107157 log .Info ("Enable the network shape of the current Muxy, please wait." )
108158
159+ fmt .Fprint (w , "Enable\n " )
109160 for _ , m := range Muxy .MiddleWares () {
110161 log .Info ("Enable type %s" , reflect .TypeOf (m ))
111162 m .Setup ()
163+ fmt .Fprintf (w , "%s.\n " , reflect .TypeOf (m ))
112164 }
113165
114166 log .Info ("Enable Muxy successfully" )
167+ fmt .Fprint (w , "Enable Muxy successfully" )
115168 case "disable" :
116169 log .Info ("Disable all rules with current Muxy, please wait." )
117170
171+ fmt .Fprint (w , "Disable\n " )
118172 for _ , m := range Muxy .MiddleWares () {
119173 log .Info ("Disable type %s" , reflect .TypeOf (m ))
120174 m .Teardown ()
175+ fmt .Fprintf (w , "%s.\n " , reflect .TypeOf (m ))
121176 }
122177
123178 log .Info ("Disable the network shape of the current Muxy successfully" )
179+ fmt .Fprint (w , "Disable Muxy successfully" )
124180 default :
125181 fmt .Fprintf (w , "Can not support %s method" , r .URL .Path [1 :])
126182 log .Debug ("Can not support %s method" , log .Colorize (log .RED , r .URL .Path [1 :]))
0 commit comments