2021-06-15 17:13:40 +08:00
package http
import (
"context"
"errors"
"net"
"net/http"
"time"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/adapter/inbound"
2024-01-02 18:26:45 +08:00
N "github.com/metacubex/mihomo/common/net"
2023-11-03 21:01:45 +08:00
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/transport/socks5"
2021-06-15 17:13:40 +08:00
)
2024-07-25 19:49:56 +08:00
func newClient ( srcConn net . Conn , tunnel C . Tunnel , additions [ ] inbound . Addition ) * http . Client { // additions using slice let caller can change its value (without size) after newClient return
2021-06-15 17:13:40 +08:00
return & http . Client {
Transport : & http . Transport {
// from http.DefaultTransport
MaxIdleConns : 100 ,
IdleConnTimeout : 90 * time . Second ,
TLSHandshakeTimeout : 10 * time . Second ,
ExpectContinueTimeout : 1 * time . Second ,
2024-07-24 14:56:46 +08:00
DisableCompression : true , // prevents the Transport add "Accept-Encoding: gzip"
2021-06-15 17:13:40 +08:00
DialContext : func ( context context . Context , network , address string ) ( net . Conn , error ) {
if network != "tcp" && network != "tcp4" && network != "tcp6" {
return nil , errors . New ( "unsupported network " + network )
}
2021-09-13 23:46:39 +08:00
dstAddr := socks5 . ParseAddr ( address )
if dstAddr == nil {
return nil , socks5 . ErrAddressNotSupported
}
2024-01-02 18:26:45 +08:00
left , right := N . Pipe ( )
2021-06-15 17:13:40 +08:00
2023-10-23 16:45:22 +08:00
go tunnel . HandleTCPConn ( inbound . NewHTTP ( dstAddr , srcConn , right , additions ... ) )
2021-06-15 17:13:40 +08:00
return left , nil
} ,
} ,
CheckRedirect : func ( req * http . Request , via [ ] * http . Request ) error {
return http . ErrUseLastResponse
} ,
}
}