|
5 | 5 | "encoding/json"
|
6 | 6 | "net"
|
7 | 7 | "runtime"
|
8 |
| - "strings" |
9 | 8 | "sync"
|
10 | 9 | "syscall"
|
11 | 10 |
|
@@ -203,12 +202,21 @@ func NewBase(opt BaseOption) *Base {
|
203 | 202 |
|
204 | 203 | type conn struct {
|
205 | 204 | N.ExtendedConn
|
206 |
| - chain C.Chain |
207 |
| - actualRemoteDestination string |
| 205 | + chain C.Chain |
| 206 | + adapterAddr string |
208 | 207 | }
|
209 | 208 |
|
210 | 209 | func (c *conn) RemoteDestination() string {
|
211 |
| - return c.actualRemoteDestination |
| 210 | + if remoteAddr := c.RemoteAddr(); remoteAddr != nil { |
| 211 | + m := C.Metadata{} |
| 212 | + if err := m.SetRemoteAddr(remoteAddr); err != nil { |
| 213 | + if m.Valid() { |
| 214 | + return m.String() |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + host, _, _ := net.SplitHostPort(c.adapterAddr) |
| 219 | + return host |
212 | 220 | }
|
213 | 221 |
|
214 | 222 | // Chains implements C.Connection
|
@@ -241,19 +249,20 @@ func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn {
|
241 | 249 | if _, ok := c.(syscall.Conn); !ok { // exclusion system conn like *net.TCPConn
|
242 | 250 | c = N.NewDeadlineConn(c) // most conn from outbound can't handle readDeadline correctly
|
243 | 251 | }
|
244 |
| - return &conn{N.NewExtendedConn(c), []string{a.Name()}, parseRemoteDestination(a.Addr())} |
| 252 | + return &conn{N.NewExtendedConn(c), []string{a.Name()}, a.Addr()} |
245 | 253 | }
|
246 | 254 |
|
247 | 255 | type packetConn struct {
|
248 | 256 | N.EnhancePacketConn
|
249 |
| - chain C.Chain |
250 |
| - adapterName string |
251 |
| - connID string |
252 |
| - actualRemoteDestination string |
| 257 | + chain C.Chain |
| 258 | + adapterName string |
| 259 | + connID string |
| 260 | + adapterAddr string |
253 | 261 | }
|
254 | 262 |
|
255 | 263 | func (c *packetConn) RemoteDestination() string {
|
256 |
| - return c.actualRemoteDestination |
| 264 | + host, _, _ := net.SplitHostPort(c.adapterAddr) |
| 265 | + return host |
257 | 266 | }
|
258 | 267 |
|
259 | 268 | // Chains implements C.Connection
|
@@ -292,19 +301,7 @@ func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn {
|
292 | 301 | if _, ok := pc.(syscall.Conn); !ok { // exclusion system conn like *net.UDPConn
|
293 | 302 | epc = N.NewDeadlineEnhancePacketConn(epc) // most conn from outbound can't handle readDeadline correctly
|
294 | 303 | }
|
295 |
| - return &packetConn{epc, []string{a.Name()}, a.Name(), utils.NewUUIDV4().String(), parseRemoteDestination(a.Addr())} |
296 |
| -} |
297 |
| - |
298 |
| -func parseRemoteDestination(addr string) string { |
299 |
| - if dst, _, err := net.SplitHostPort(addr); err == nil { |
300 |
| - return dst |
301 |
| - } else { |
302 |
| - if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") { |
303 |
| - return dst |
304 |
| - } else { |
305 |
| - return "" |
306 |
| - } |
307 |
| - } |
| 304 | + return &packetConn{epc, []string{a.Name()}, a.Name(), utils.NewUUIDV4().String(), a.Addr()} |
308 | 305 | }
|
309 | 306 |
|
310 | 307 | type AddRef interface {
|
|
0 commit comments