frp/tests/mock/http_server.go

111 lines
2.2 KiB
Go
Raw Normal View History

2018-07-11 23:27:47 +08:00
package mock
2017-03-10 00:52:32 +08:00
import (
"fmt"
2018-01-23 14:49:04 +08:00
"log"
2018-12-09 21:56:46 +08:00
"net"
2017-03-10 00:52:32 +08:00
"net/http"
2018-01-22 14:16:46 +08:00
"regexp"
2017-12-18 19:35:09 +08:00
"strings"
2018-01-23 14:49:04 +08:00
2018-07-11 23:27:47 +08:00
"github.com/fatedier/frp/tests/consts"
2018-01-23 14:49:04 +08:00
"github.com/gorilla/websocket"
2017-03-10 00:52:32 +08:00
)
2020-05-24 17:48:37 +08:00
type HTTPServer struct {
2018-12-09 21:56:46 +08:00
l net.Listener
port int
handler http.HandlerFunc
}
2020-05-24 17:48:37 +08:00
func NewHTTPServer(port int, handler http.HandlerFunc) *HTTPServer {
return &HTTPServer{
2018-12-09 21:56:46 +08:00
port: port,
handler: handler,
}
}
2020-05-24 17:48:37 +08:00
func (hs *HTTPServer) Start() error {
2018-12-09 21:56:46 +08:00
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", hs.port))
if err != nil {
fmt.Printf("http server listen error: %v\n", err)
return err
}
hs.l = l
go http.Serve(l, http.HandlerFunc(hs.handler))
return nil
}
2020-05-24 17:48:37 +08:00
func (hs *HTTPServer) Stop() {
2018-12-09 21:56:46 +08:00
hs.l.Close()
}
2018-01-23 14:49:04 +08:00
var upgrader = websocket.Upgrader{}
2020-05-24 17:48:37 +08:00
func StartHTTPServer(port int) {
http.HandleFunc("/", handleHTTP)
2018-01-23 14:49:04 +08:00
http.HandleFunc("/ws", handleWebSocket)
2018-07-11 23:27:47 +08:00
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
2017-03-10 00:52:32 +08:00
}
2018-01-23 14:49:04 +08:00
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
break
}
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
}
}
2020-05-24 17:48:37 +08:00
func handleHTTP(w http.ResponseWriter, r *http.Request) {
2018-05-20 23:55:22 +08:00
if r.Header.Get("X-From-Where") == "frp" {
w.Header().Set("X-Header-Set", "true")
}
2018-01-22 14:16:46 +08:00
match, err := regexp.Match(`.*\.sub\.com`, []byte(r.Host))
if err != nil {
w.WriteHeader(500)
return
}
if match {
w.WriteHeader(200)
w.Write([]byte(r.Host))
return
}
if strings.HasPrefix(r.Host, "127.0.0.1") || strings.HasPrefix(r.Host, "test2.frp.com") ||
strings.HasPrefix(r.Host, "test5.frp.com") || strings.HasPrefix(r.Host, "test6.frp.com") ||
strings.HasPrefix(r.Host, "test.frp1.com") || strings.HasPrefix(r.Host, "new.test.frp1.com") {
2017-12-18 19:35:09 +08:00
w.WriteHeader(200)
2018-07-11 23:27:47 +08:00
w.Write([]byte(consts.TEST_HTTP_NORMAL_STR))
2017-12-18 19:35:09 +08:00
} else if strings.Contains(r.Host, "test3.frp.com") {
w.WriteHeader(200)
if strings.Contains(r.URL.Path, "foo") {
2018-07-11 23:27:47 +08:00
w.Write([]byte(consts.TEST_HTTP_FOO_STR))
2017-12-18 19:35:09 +08:00
} else if strings.Contains(r.URL.Path, "bar") {
2018-07-11 23:27:47 +08:00
w.Write([]byte(consts.TEST_HTTP_BAR_STR))
2017-12-18 19:35:09 +08:00
} else {
2018-07-11 23:27:47 +08:00
w.Write([]byte(consts.TEST_HTTP_NORMAL_STR))
2017-12-18 19:35:09 +08:00
}
} else {
w.WriteHeader(404)
}
return
2017-03-10 00:52:32 +08:00
}