@@ -14,6 +14,14 @@ import (
1414 "time"
1515)
1616
17+ var (
18+ errorResp = "HTTP/1.0 200 OK\r \n " +
19+ "Connection: close\r \n " +
20+ "Content-Type: application/json;charset=UTF-8\r \n " +
21+ "\r \n " +
22+ `{"data":{"flag":"1","map":{"CUW":"重庆北","HKN":"汉口","WCN":"武昌"},"result":["|预订|2400000Z490F|Z49|BXP|CDW|HKN|CUW|22:20|05:32|07:12|N|g%2Bd3SnPhBrp6gzy5Eq4zATL6rGgQn4nznpuJl5niuOcsnDutccVaBIqfJp4%3D|20190101|3|P4|05|08|0|0||||无|||无||无|无|||||10401030|1413|0|0"]},"httpstatus":200,"messages":"","status":true}`
23+ )
24+
1725const (
1826 Version = "1.1"
1927 ONE_DAY = 24 * time .Hour
@@ -59,15 +67,6 @@ func (handler *HandlerWrapper) ServeHTTP(resp http.ResponseWriter, req *http.Req
5967 }
6068}
6169
62- type myDialer struct {
63- d * net.Dialer
64- ConnectDial func (network string , addr string ) (net.Conn , error )
65- }
66-
67- func (d * myDialer ) Dial (network , address string ) (net.Conn , error ) {
68- return d .ConnectDial (network , address )
69- }
70-
7170// DumpHTTPAndHTTPS function to dump the HTTP request header and body
7271func (handler * HandlerWrapper ) DumpHTTPAndHTTPS (resp http.ResponseWriter , req * http.Request ) {
7372 req .Header .Del ("Proxy-Connection" )
@@ -105,23 +104,35 @@ func (handler *HandlerWrapper) DumpHTTPAndHTTPS(resp http.ResponseWriter, req *h
105104 if ! matched {
106105 host += ":443"
107106 }
108- if host == "kyfw.12306.cn:443" && strings .HasPrefix (req .RequestURI , "/otn/leftTicket/query" ) {
109- host = <- cdnChan
110- logger .Println ("current CDN is:" , host )
107+ if host == "kyfw.12306.cn:443" {
108+ host = fastest
109+ if strings .HasPrefix (req .RequestURI , * queryURL ) {
110+ host = <- cdnChan
111+ logger .Println ("current CDN is:" , host )
112+ connOut , err = tls .DialWithDialer (& net.Dialer {
113+ Timeout : 1 * time .Second ,
114+ KeepAlive : 0 ,
115+ }, "tcp" , host , handler .tlsConfig .ServerTLSConfig )
116+ if err != nil {
117+ logger .Println ("Dial to" , host , "error:" , err )
118+ connHj .Write ([]byte (errorResp ))
119+ return
120+ }
121+ } else {
122+ connOut , err = tls .Dial ("tcp" , host , handler .tlsConfig .ServerTLSConfig )
123+ if err != nil {
124+ logger .Println ("Dial to" , host , "error:" , err )
125+ return
126+ }
127+ }
128+ } else {
129+ connOut , err = tls .Dial ("tcp" , host , handler .tlsConfig .ServerTLSConfig )
130+ if err != nil {
131+ logger .Println ("Dial to" , host , "error:" , err )
132+ return
133+ }
111134 }
112135
113- connOut , err = tls .DialWithDialer (& net.Dialer {
114- Timeout : 1 * time .Second ,
115- KeepAlive : 0 ,
116- }, "tcp" , host , handler .tlsConfig .ServerTLSConfig )
117- if err != nil {
118- logger .Println ("Dial to" , host , "error:" , err )
119- connHj .Write ([]byte ("HTTP/1.0 200 OK\r \n " +
120- "Connection: close\r \n " +
121- "\r \n " +
122- "Body here\n " ))
123- return
124- }
125136 }
126137
127138 // Write writes an HTTP/1.1 request, which is the header and body, in wire format. This method consults the following fields of the request:
@@ -136,22 +147,26 @@ func (handler *HandlerWrapper) DumpHTTPAndHTTPS(resp http.ResponseWriter, req *h
136147 */
137148 if err = req .Write (connOut ); err != nil {
138149 logger .Println ("send to server error" , err )
150+ connHj .Write ([]byte (errorResp ))
139151 return
140152 }
141153
142154 respFromRemote , err := http .ReadResponse (bufio .NewReader (connOut ), req )
143155 if err != nil && err != io .EOF {
144156 logger .Println ("Fail to read response from remote server." , err )
157+ connHj .Write ([]byte (errorResp ))
145158 }
146159
147160 respDump , err := httputil .DumpResponse (respFromRemote , true )
148161 if err != nil {
149162 logger .Println ("Fail to dump the response." , err )
163+ connHj .Write ([]byte (errorResp ))
150164 }
151165 // Send remote response back to client
152166 _ , err = connHj .Write (respDump )
153167 if err != nil {
154168 logger .Println ("Fail to send response back to client." , err )
169+ connHj .Write ([]byte (errorResp ))
155170 }
156171
157172 <- ch
0 commit comments