http_server.go 975 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package tests
  2. import (
  3. "fmt"
  4. "net/http"
  5. "regexp"
  6. "strings"
  7. )
  8. func StartHttpServer() {
  9. http.HandleFunc("/", request)
  10. http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", TEST_HTTP_PORT), nil)
  11. }
  12. func request(w http.ResponseWriter, r *http.Request) {
  13. match, err := regexp.Match(`.*\.sub\.com`, []byte(r.Host))
  14. if err != nil {
  15. w.WriteHeader(500)
  16. return
  17. }
  18. if match {
  19. w.WriteHeader(200)
  20. w.Write([]byte(r.Host))
  21. return
  22. }
  23. if strings.Contains(r.Host, "127.0.0.1") || strings.Contains(r.Host, "test2.frp.com") ||
  24. strings.Contains(r.Host, "test5.frp.com") {
  25. w.WriteHeader(200)
  26. w.Write([]byte(TEST_HTTP_NORMAL_STR))
  27. } else if strings.Contains(r.Host, "test3.frp.com") {
  28. w.WriteHeader(200)
  29. if strings.Contains(r.URL.Path, "foo") {
  30. w.Write([]byte(TEST_HTTP_FOO_STR))
  31. } else if strings.Contains(r.URL.Path, "bar") {
  32. w.Write([]byte(TEST_HTTP_BAR_STR))
  33. } else {
  34. w.Write([]byte(TEST_HTTP_NORMAL_STR))
  35. }
  36. } else {
  37. w.WriteHeader(404)
  38. }
  39. return
  40. }