frp/tests/http_server.go

46 lines
975 B
Go
Raw Normal View History

2017-03-10 00:52:32 +08:00
package tests
import (
"fmt"
"net/http"
2018-01-22 14:16:46 +08:00
"regexp"
2017-12-18 19:35:09 +08:00
"strings"
2017-03-10 00:52:32 +08:00
)
func StartHttpServer() {
http.HandleFunc("/", request)
2017-12-18 19:35:09 +08:00
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", TEST_HTTP_PORT), nil)
2017-03-10 00:52:32 +08:00
}
func request(w http.ResponseWriter, r *http.Request) {
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
}
2017-12-18 19:35:09 +08:00
if strings.Contains(r.Host, "127.0.0.1") || strings.Contains(r.Host, "test2.frp.com") ||
strings.Contains(r.Host, "test5.frp.com") {
w.WriteHeader(200)
w.Write([]byte(TEST_HTTP_NORMAL_STR))
} else if strings.Contains(r.Host, "test3.frp.com") {
w.WriteHeader(200)
if strings.Contains(r.URL.Path, "foo") {
w.Write([]byte(TEST_HTTP_FOO_STR))
} else if strings.Contains(r.URL.Path, "bar") {
w.Write([]byte(TEST_HTTP_BAR_STR))
} else {
w.Write([]byte(TEST_HTTP_NORMAL_STR))
}
} else {
w.WriteHeader(404)
}
return
2017-03-10 00:52:32 +08:00
}