diff --git a/proxy/http/config.go b/proxy/http/config.go index a025358a3..8ee515332 100644 --- a/proxy/http/config.go +++ b/proxy/http/config.go @@ -1,22 +1,21 @@ package http -import ( - "crypto/tls" - - v2net "github.com/v2ray/v2ray-core/common/net" -) +import "crypto/tls" +// CertificateConfig is the config for TLS certificates used in HTTP proxy. type CertificateConfig struct { Domain string Certificate tls.Certificate } -type TlsConfig struct { +// TlsConfig is the config for TLS connections. +type TLSConfig struct { Enabled bool Certs []*CertificateConfig } -func (this *TlsConfig) GetConfig() *tls.Config { +// GetConfig returns corresponding tls.Config. +func (this *TLSConfig) GetConfig() *tls.Config { if !this.Enabled { return nil } @@ -35,19 +34,11 @@ func (this *TlsConfig) GetConfig() *tls.Config { return config } +// Config for HTTP proxy server. type Config struct { - OwnHosts []v2net.Address - TlsConfig *TlsConfig -} - -func (this *Config) IsOwnHost(host v2net.Address) bool { - for _, ownHost := range this.OwnHosts { - if ownHost.Equals(host) { - return true - } - } - return false + TLSConfig *TLSConfig } +// ClientConfig for HTTP proxy client. type ClientConfig struct { } diff --git a/proxy/http/config_json.go b/proxy/http/config_json.go index 10bf4aec4..442373356 100644 --- a/proxy/http/config_json.go +++ b/proxy/http/config_json.go @@ -6,10 +6,10 @@ import ( "crypto/tls" "encoding/json" - v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/proxy/internal/config" ) +// UnmarshalJSON implements json.Unmarshaler func (this *CertificateConfig) UnmarshalJSON(data []byte) error { type JsonConfig struct { Domain string `json:"domain"` @@ -30,7 +30,8 @@ func (this *CertificateConfig) UnmarshalJSON(data []byte) error { return nil } -func (this *TlsConfig) UnmarshalJSON(data []byte) error { +// UnmarshalJSON implements json.Unmarshaler +func (this *TLSConfig) UnmarshalJSON(data []byte) error { type JsonConfig struct { Enabled bool `json:"enable"` Certs []*CertificateConfig `json:"certs"` @@ -45,26 +46,17 @@ func (this *TlsConfig) UnmarshalJSON(data []byte) error { return nil } +// UnmarshalJSON implements json.Unmarshaler func (this *Config) UnmarshalJSON(data []byte) error { type JsonConfig struct { - Hosts []v2net.AddressJson `json:"ownHosts"` - Tls *TlsConfig `json:"tls"` + Tls *TLSConfig `json:"tls"` } jsonConfig := new(JsonConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { return err } - this.OwnHosts = make([]v2net.Address, len(jsonConfig.Hosts), len(jsonConfig.Hosts)+1) - for idx, host := range jsonConfig.Hosts { - this.OwnHosts[idx] = host.Address - } - v2rayHost := v2net.DomainAddress("local.v2ray.com") - if !this.IsOwnHost(v2rayHost) { - this.OwnHosts = append(this.OwnHosts, v2rayHost) - } - - this.TlsConfig = jsonConfig.Tls + this.TLSConfig = jsonConfig.Tls return nil } diff --git a/proxy/http/config_json_test.go b/proxy/http/config_json_test.go index 854bdf265..45c1bb0e1 100644 --- a/proxy/http/config_json_test.go +++ b/proxy/http/config_json_test.go @@ -1,31 +1,3 @@ // +build json package http_test - -import ( - "encoding/json" - "testing" - - v2net "github.com/v2ray/v2ray-core/common/net" - . "github.com/v2ray/v2ray-core/proxy/http" - "github.com/v2ray/v2ray-core/testing/assert" -) - -func TestOwnHosts(t *testing.T) { - assert := assert.On(t) - - rawJson := `{ - "ownHosts": [ - "127.0.0.1", - "google.com" - ] - }` - - config := new(Config) - err := json.Unmarshal([]byte(rawJson), config) - assert.Error(err).IsNil() - assert.Bool(config.IsOwnHost(v2net.IPAddress([]byte{127, 0, 0, 1}))).IsTrue() - assert.Bool(config.IsOwnHost(v2net.DomainAddress("google.com"))).IsTrue() - assert.Bool(config.IsOwnHost(v2net.DomainAddress("local.v2ray.com"))).IsTrue() - assert.Bool(config.IsOwnHost(v2net.DomainAddress("v2ray.com"))).IsFalse() -} diff --git a/proxy/http/server.go b/proxy/http/server.go index ceb05f002..83da7cdd8 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -22,7 +22,8 @@ import ( "github.com/v2ray/v2ray-core/transport/ray" ) -type HttpProxyServer struct { +// Server is a HTTP proxy server. +type Server struct { sync.Mutex accepting bool packetDispatcher dispatcher.PacketDispatcher @@ -32,18 +33,18 @@ type HttpProxyServer struct { listeningAddress v2net.Address } -func NewHttpProxyServer(config *Config, packetDispatcher dispatcher.PacketDispatcher) *HttpProxyServer { - return &HttpProxyServer{ +func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher) *Server { + return &Server{ packetDispatcher: packetDispatcher, config: config, } } -func (this *HttpProxyServer) Port() v2net.Port { +func (this *Server) Port() v2net.Port { return this.listeningPort } -func (this *HttpProxyServer) Close() { +func (this *Server) Close() { this.accepting = false if this.tcpListener != nil { this.Lock() @@ -53,7 +54,7 @@ func (this *HttpProxyServer) Close() { } } -func (this *HttpProxyServer) Listen(address v2net.Address, port v2net.Port) error { +func (this *Server) Listen(address v2net.Address, port v2net.Port) error { if this.accepting { if this.listeningPort == port && this.listeningAddress.Equals(address) { return nil @@ -64,9 +65,9 @@ func (this *HttpProxyServer) Listen(address v2net.Address, port v2net.Port) erro this.listeningPort = port this.listeningAddress = address - var tlsConfig *tls.Config = nil - if this.config.TlsConfig != nil { - tlsConfig = this.config.TlsConfig.GetConfig() + var tlsConfig *tls.Config + if this.config.TLSConfig != nil { + tlsConfig = this.config.TLSConfig.GetConfig() } tcpListener, err := hub.ListenTCP(address, port, this.handleConnection, tlsConfig) if err != nil { @@ -103,7 +104,7 @@ func parseHost(rawHost string, defaultPort v2net.Port) (v2net.Destination, error return v2net.TCPDestination(v2net.DomainAddress(host), port), nil } -func (this *HttpProxyServer) handleConnection(conn *hub.Connection) { +func (this *Server) handleConnection(conn *hub.Connection) { defer conn.Close() reader := bufio.NewReader(conn) @@ -133,7 +134,7 @@ func (this *HttpProxyServer) handleConnection(conn *hub.Connection) { } } -func (this *HttpProxyServer) handleConnect(request *http.Request, destination v2net.Destination, reader io.Reader, writer io.Writer) { +func (this *Server) handleConnect(request *http.Request, destination v2net.Destination, reader io.Reader, writer io.Writer) { response := &http.Response{ Status: "200 OK", StatusCode: 200, @@ -155,7 +156,7 @@ func (this *HttpProxyServer) handleConnect(request *http.Request, destination v2 this.transport(reader, writer, ray) } -func (this *HttpProxyServer) transport(input io.Reader, output io.Writer, ray ray.InboundRay) { +func (this *Server) transport(input io.Reader, output io.Writer, ray ray.InboundRay) { var wg sync.WaitGroup wg.Add(2) defer wg.Wait() @@ -204,7 +205,7 @@ func StripHopByHopHeaders(request *http.Request) { } } -func (this *HttpProxyServer) handlePlainHTTP(request *http.Request, dest v2net.Destination, reader *bufio.Reader, writer io.Writer) { +func (this *Server) handlePlainHTTP(request *http.Request, dest v2net.Destination, reader *bufio.Reader, writer io.Writer) { if len(request.URL.Host) <= 0 { hdr := http.Header(make(map[string][]string)) hdr.Set("Connection", "close") @@ -273,7 +274,7 @@ func init() { if !space.HasApp(dispatcher.APP_ID) { return nil, internal.ErrorBadConfiguration } - return NewHttpProxyServer( + return NewServer( rawConfig.(*Config), space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)), nil }) diff --git a/proxy/http/server_test.go b/proxy/http/server_test.go index 36c33b385..b4b1b880d 100644 --- a/proxy/http/server_test.go +++ b/proxy/http/server_test.go @@ -52,7 +52,7 @@ func TestNormalGetRequest(t *testing.T) { testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil) - httpProxy := NewHttpProxyServer(&Config{}, testPacketDispatcher) + httpProxy := NewServer(&Config{}, testPacketDispatcher) defer httpProxy.Close() port := v2nettesting.PickPort()