2016-01-24 15:48:38 -05:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2016-01-24 15:48:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2017-08-29 06:56:57 -04:00
|
|
|
Port net.Port
|
2016-01-24 15:48:38 -05:00
|
|
|
PathHandler map[string]http.HandlerFunc
|
2017-11-08 19:06:40 -05:00
|
|
|
server *http.Server
|
2016-01-24 15:48:38 -05:00
|
|
|
}
|
|
|
|
|
2017-11-08 19:06:40 -05:00
|
|
|
func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
2016-01-24 15:48:38 -05:00
|
|
|
if req.URL.Path == "/" {
|
|
|
|
resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
resp.WriteHeader(http.StatusOK)
|
|
|
|
resp.Write([]byte("Home"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:06:40 -05:00
|
|
|
handler, found := s.PathHandler[req.URL.Path]
|
2016-01-24 15:48:38 -05:00
|
|
|
if found {
|
|
|
|
handler(resp, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:06:40 -05:00
|
|
|
func (s *Server) Start() (net.Destination, error) {
|
|
|
|
s.server = &http.Server{
|
|
|
|
Addr: "127.0.0.1:" + s.Port.String(),
|
|
|
|
Handler: s,
|
|
|
|
}
|
|
|
|
go s.server.ListenAndServe()
|
2020-10-11 07:22:46 -04:00
|
|
|
return net.TCPDestination(net.LocalHostIP, s.Port), nil
|
2016-01-24 15:48:38 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (s *Server) Close() error {
|
|
|
|
return s.server.Close()
|
2016-01-24 15:48:38 -05:00
|
|
|
}
|