style: rename controllers to login, cancel internal/pkg
This commit is contained in:
53
internal/http_client/http.go
Normal file
53
internal/http_client/http.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package http_client
|
||||
|
||||
import (
|
||||
"github.com/Mmx233/BitSrunLoginGo/internal/config"
|
||||
"github.com/Mmx233/BitSrunLoginGo/internal/config/flags"
|
||||
"github.com/Mmx233/BitSrunLoginGo/internal/config/keys"
|
||||
"github.com/Mmx233/BitSrunLoginGo/tools"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var _DefaultClient *http.Client
|
||||
|
||||
var _EthClientMap map[string]*http.Client
|
||||
|
||||
func init() {
|
||||
logger := config.Logger.WithField(keys.LogComponent, "init http")
|
||||
if config.Settings.Basic.Interfaces == "" {
|
||||
var eth *tools.Eth
|
||||
if flags.Interface != "" {
|
||||
netEth, err := net.InterfaceByName(flags.Interface)
|
||||
if err != nil {
|
||||
logger.Warnf("获取指定网卡 %s 失败,使用默认网卡: %v", flags.Interface, err)
|
||||
} else {
|
||||
eth, err = tools.ConvertInterface(logger, *netEth)
|
||||
if err != nil {
|
||||
logger.Warnf("获取指定网卡 %s ip 地址失败,使用默认网卡: %v", flags.Interface, err)
|
||||
} else if eth == nil {
|
||||
logger.Warnf("指定网卡 %s 无可用 ip 地址,使用默认网卡", flags.Interface)
|
||||
} else {
|
||||
logger.Debugf("使用指定网卡 %s ip: %s", eth.Name, eth.Addr.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_DefaultClient = CreateClientFromEth(eth)
|
||||
} else {
|
||||
_EthClientMap = make(map[string]*http.Client)
|
||||
}
|
||||
}
|
||||
|
||||
func ClientSelect(eth *tools.Eth) *http.Client {
|
||||
if _DefaultClient != nil {
|
||||
return _DefaultClient
|
||||
}
|
||||
if client, ok := _EthClientMap[eth.Name]; ok {
|
||||
return client
|
||||
} else {
|
||||
client = CreateClientFromEth(eth)
|
||||
_EthClientMap[eth.Name] = client
|
||||
return client
|
||||
}
|
||||
}
|
||||
33
internal/http_client/http_default.go
Normal file
33
internal/http_client/http_default.go
Normal file
@@ -0,0 +1,33 @@
|
||||
//go:build !linux
|
||||
|
||||
package http_client
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"github.com/Mmx233/BitSrunLoginGo/internal/config"
|
||||
"github.com/Mmx233/BitSrunLoginGo/tools"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CreateClientFromEth(eth *tools.Eth) *http.Client {
|
||||
var addr net.Addr
|
||||
if eth != nil {
|
||||
addr = eth.Addr
|
||||
}
|
||||
|
||||
return &http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: config.Timeout,
|
||||
LocalAddr: addr,
|
||||
}).DialContext,
|
||||
TLSHandshakeTimeout: config.Timeout,
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: config.Settings.Basic.SkipCertVerify,
|
||||
},
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
},
|
||||
Timeout: config.Timeout,
|
||||
}
|
||||
}
|
||||
43
internal/http_client/http_linux.go
Normal file
43
internal/http_client/http_linux.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package http_client
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"github.com/Mmx233/BitSrunLoginGo/internal/config"
|
||||
"github.com/Mmx233/BitSrunLoginGo/tools"
|
||||
"net"
|
||||
"net/http"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func CreateClientFromEth(eth *tools.Eth) *http.Client {
|
||||
dialer := net.Dialer{
|
||||
Timeout: config.Timeout,
|
||||
}
|
||||
if eth != nil {
|
||||
dialer.LocalAddr = eth.Addr
|
||||
ethName := eth.Name
|
||||
dialer.Control = func(network string, address string, c syscall.RawConn) error {
|
||||
var opErr error
|
||||
fn := func(fd uintptr) {
|
||||
opErr = syscall.SetsockoptString(int(fd), syscall.SOL_SOCKET, syscall.SO_BINDTODEVICE, ethName)
|
||||
}
|
||||
if err := c.Control(fn); err != nil {
|
||||
return err
|
||||
}
|
||||
if opErr != nil {
|
||||
return opErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return &http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: dialer.DialContext,
|
||||
TLSHandshakeTimeout: config.Timeout,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: config.Settings.Basic.SkipCertVerify},
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
},
|
||||
Timeout: config.Timeout,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user