From 5c8bb24121f90377c1af91021c7d54bcd3719a0a Mon Sep 17 00:00:00 2001
From: Dreamacro <305009791@qq.com>
Date: Mon, 18 Feb 2019 20:14:18 +0800
Subject: [PATCH] Fix: crash when directly request proxy server

---
 proxy/http/server.go |  4 ++--
 tunnel/tunnel.go     | 10 +++++-----
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/proxy/http/server.go b/proxy/http/server.go
index e66f5493..7c20e20f 100644
--- a/proxy/http/server.go
+++ b/proxy/http/server.go
@@ -5,7 +5,7 @@ import (
 	"net"
 	"net/http"
 
-	"github.com/Dreamacro/clash/adapters/inbound"
+	adapters "github.com/Dreamacro/clash/adapters/inbound"
 	"github.com/Dreamacro/clash/log"
 	"github.com/Dreamacro/clash/tunnel"
 )
@@ -56,7 +56,7 @@ func (l *HttpListener) Address() string {
 func handleConn(conn net.Conn) {
 	br := bufio.NewReader(conn)
 	request, err := http.ReadRequest(br)
-	if err != nil {
+	if err != nil || !request.URL.IsAbs() {
 		conn.Close()
 		return
 	}
diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go
index 77e81058..d98ec40a 100644
--- a/tunnel/tunnel.go
+++ b/tunnel/tunnel.go
@@ -115,6 +115,11 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
 	defer localConn.Close()
 	metadata := localConn.Metadata()
 
+	if !metadata.Valid() {
+		log.Warnln("[Metadata] not valid: %#v", metadata)
+		return
+	}
+
 	if t.needLookupIP(metadata) {
 		host, exist := t.resolver.IPToHost(*metadata.IP)
 		if exist {
@@ -138,11 +143,6 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
 		}
 	}
 
-	if !metadata.Valid() {
-		log.Warnln("[Metadata] not valid: %#v", metadata)
-		return
-	}
-
 	remoConn, err := proxy.Generator(metadata)
 	if err != nil {
 		log.Warnln("Proxy[%s] connect [%s --> %s] error: %s", proxy.Name(), metadata.SourceIP.String(), metadata.String(), err.Error())