http.go 793 B

123456789101112131415161718192021222324252627282930313233343536
  1. package tcp
  2. import (
  3. "net/http"
  4. v2net "github.com/v2ray/v2ray-core/common/net"
  5. )
  6. type Server struct {
  7. Port v2net.Port
  8. PathHandler map[string]http.HandlerFunc
  9. accepting bool
  10. }
  11. func (server *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
  12. if req.URL.Path == "/" {
  13. resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
  14. resp.WriteHeader(http.StatusOK)
  15. resp.Write([]byte("Home"))
  16. return
  17. }
  18. handler, found := server.PathHandler[req.URL.Path]
  19. if found {
  20. handler(resp, req)
  21. }
  22. }
  23. func (server *Server) Start() (v2net.Destination, error) {
  24. go http.ListenAndServe("127.0.0.1:"+server.Port.String(), server)
  25. return v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(server.Port)), nil
  26. }
  27. func (this *Server) Close() {
  28. this.accepting = false
  29. }