style: 变量命名

This commit is contained in:
Mmx233
2023-08-28 22:49:17 +08:00
parent 1df6000c71
commit e77448eeb9
10 changed files with 130 additions and 130 deletions

View File

@@ -19,13 +19,13 @@ func main() {
if config.Settings.Basic.Interfaces == "" { //单网卡
var eth *tools.Eth
if flags.Interface != "" {
netEth, e := net.InterfaceByName(flags.Interface)
if e != nil {
log.Warnf("获取指定网卡 %s 失败,使用默认网卡: %v", flags.Interface, e)
netEth, err := net.InterfaceByName(flags.Interface)
if err != nil {
log.Warnf("获取指定网卡 %s 失败,使用默认网卡: %v", flags.Interface, err)
} else {
eth, e = tools.ConvertInterface(*netEth)
if e != nil {
log.Warnf("获取指定网卡 %s ip 地址失败,使用默认网卡: %v", flags.Interface, e)
eth, err = tools.ConvertInterface(*netEth)
if err != nil {
log.Warnf("获取指定网卡 %s ip 地址失败,使用默认网卡: %v", flags.Interface, err)
} else if eth == nil {
log.Warnf("指定网卡 %s 无可用 ip 地址,使用默认网卡", flags.Interface)
} else {

View File

@@ -20,18 +20,18 @@ func initLog() {
if !strings.HasSuffix(Settings.Log.FilePath, "/") {
Settings.Log.FilePath += "/"
}
e := os.MkdirAll(Settings.Log.FilePath, os.ModePerm)
if e != nil {
log.Fatalln(e)
err := os.MkdirAll(Settings.Log.FilePath, os.ModePerm)
if err != nil {
log.Fatalln(err)
}
if Settings.Log.FileName == "" {
Settings.Log.FileName = time.Now().Format("2006.01.02-15.04.05") + ".log"
}
f, e := os.OpenFile(Settings.Log.FilePath+Settings.Log.FileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if e != nil {
log.Fatalln(e)
f, err := os.OpenFile(Settings.Log.FilePath+Settings.Log.FileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalln(err)
}
//设置双重输出

View File

@@ -21,18 +21,18 @@ func Guardian() {
_ = recover()
}()
if config.Settings.Basic.Interfaces == "" { //单网卡
e := Login(nil, true)
if e != nil {
log.Errorln("登录出错: ", e)
err := Login(nil, true)
if err != nil {
log.Errorln("登录出错: ", err)
}
} else { //多网卡
interfaces, e := tools.GetInterfaceAddr(config.Settings.Basic.Interfaces)
if e == nil {
interfaces, err := tools.GetInterfaceAddr(config.Settings.Basic.Interfaces)
if err == nil {
for _, eth := range interfaces {
log.Debugf("使用 %s 网口登录 ", eth.Name)
e = Login(&eth, true)
if e != nil {
log.Errorln("网口 ", eth.Name+" 登录出错: ", e)
err = Login(&eth, true)
if err != nil {
log.Errorln("网口 ", eth.Name+" 登录出错: ", err)
}
}
}

View File

@@ -50,9 +50,9 @@ func Login(eth *tools.Eth, debugOutput bool) error {
output("正在获取登录状态")
online, ip, e := srunClient.LoginStatus()
if e != nil {
return e
online, ip, err := srunClient.LoginStatus()
if err != nil {
return err
}
log.Debugln("认证客户端 ip: ", ip)
@@ -72,8 +72,8 @@ func Login(eth *tools.Eth, debugOutput bool) error {
} else {
log.Infoln("检测到用户未登录,开始尝试登录...")
if e = srunClient.DoLogin(ip); e != nil {
return e
if err = srunClient.DoLogin(ip); err != nil {
return err
}
log.Infoln("登录成功~")

View File

@@ -28,9 +28,9 @@ func New(ttl uint, conf map[string]interface{}, Http *http.Client) (*DnsProvider
TTL: ttl,
Http: tool.NewHttpTool(Http),
}
e := dnsUtil.DecodeConfig(conf, &p)
if e != nil {
return nil, e
err := dnsUtil.DecodeConfig(conf, &p)
if err != nil {
return nil, err
}
if p.AccessKeyId == "" || p.AccessKeySecret == "" {
@@ -74,9 +74,9 @@ func (a DnsProvider) SendRequest(Type, Action string, data map[string]interface{
}
mac := hmac.New(sha1.New, []byte(a.AccessKeySecret+"&"))
_, e := mac.Write([]byte(signStr))
if e != nil {
return nil, e
_, err := mac.Write([]byte(signStr))
if err != nil {
return nil, err
}
data["Signature"] = base64.StdEncoding.EncodeToString(mac.Sum(nil))
@@ -86,16 +86,16 @@ func (a DnsProvider) SendRequest(Type, Action string, data map[string]interface{
reqOpt.Body = data
}
resp, e := a.Http.Request(Type, &reqOpt)
if e != nil {
return nil, e
resp, err := a.Http.Request(Type, &reqOpt)
if err != nil {
return nil, err
}
if resp.StatusCode > 299 {
defer resp.Body.Close()
var res Response
if e = json.NewDecoder(resp.Body).Decode(&res); e != nil {
return nil, e
if err = json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return nil, errors.New(res.Message)
@@ -105,21 +105,21 @@ func (a DnsProvider) SendRequest(Type, Action string, data map[string]interface{
}
func (a DnsProvider) DomainRecordStatus(subDomain, rootDomain string) (*DomainStatus, bool, error) {
resp, e := a.SendRequest("GET", "DescribeDomainRecords", map[string]interface{}{
resp, err := a.SendRequest("GET", "DescribeDomainRecords", map[string]interface{}{
"DomainName": rootDomain,
"SearchMode": "EXACT",
"KeyWord": subDomain,
"PageSize": 1,
"Type": "A",
})
if e != nil {
return nil, false, e
if err != nil {
return nil, false, err
}
defer resp.Body.Close()
var res DomainStatusRes
if e = json.NewDecoder(resp.Body).Decode(&res); e != nil {
return nil, false, e
if err = json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, false, err
}
if res.TotalCount == 0 || len(res.DomainRecords.Record) == 0 {
@@ -130,15 +130,15 @@ func (a DnsProvider) DomainRecordStatus(subDomain, rootDomain string) (*DomainSt
}
func (a DnsProvider) UpdateRecord(RecordId, subDomain, ip string) error {
resp, e := a.SendRequest("POST", "UpdateDomainRecord", map[string]interface{}{
resp, err := a.SendRequest("POST", "UpdateDomainRecord", map[string]interface{}{
"RecordId": RecordId,
"RR": subDomain,
"Type": "A",
"Value": ip,
"TTL": a.TTL,
})
if e != nil {
return e
if err != nil {
return err
}
defer resp.Body.Close()
@@ -146,15 +146,15 @@ func (a DnsProvider) UpdateRecord(RecordId, subDomain, ip string) error {
}
func (a DnsProvider) NewRecord(subDomain, rootDomain, ip string) error {
resp, e := a.SendRequest("POST", "AddDomainRecord", map[string]interface{}{
resp, err := a.SendRequest("POST", "AddDomainRecord", map[string]interface{}{
"DomainName": rootDomain,
"RR": subDomain,
"Type": "A",
"Value": ip,
"TTL": a.TTL,
})
if e != nil {
return e
if err != nil {
return err
}
defer resp.Body.Close()
@@ -162,14 +162,14 @@ func (a DnsProvider) NewRecord(subDomain, rootDomain, ip string) error {
}
func (a DnsProvider) SetDomainRecord(domain, ip string) error {
subDomain, rootDomain, e := dnsUtil.DecodeDomain(domain)
if e != nil {
return e
subDomain, rootDomain, err := dnsUtil.DecodeDomain(domain)
if err != nil {
return err
}
record, exist, e := a.DomainRecordStatus(subDomain, rootDomain)
if e != nil {
return e
record, exist, err := a.DomainRecordStatus(subDomain, rootDomain)
if err != nil {
return err
}
if exist {

View File

@@ -20,9 +20,9 @@ func New(ttl int, conf map[string]interface{}, Http *http.Client) (*DnsProvider,
var p = DnsProvider{
TTL: ttl,
}
e := dnsUtil.DecodeConfig(conf, &p)
if e != nil {
return nil, e
err := dnsUtil.DecodeConfig(conf, &p)
if err != nil {
return nil, err
}
if p.Zone == "" {
@@ -34,36 +34,36 @@ func New(ttl int, conf map[string]interface{}, Http *http.Client) (*DnsProvider,
return nil, errors.New("cloudflare token 不能为空")
}
p.Api, e = cloudflare.NewWithAPIToken(p.Token, cloudflare.HTTPClient(Http))
return &p, e
p.Api, err = cloudflare.NewWithAPIToken(p.Token, cloudflare.HTTPClient(Http))
return &p, err
}
func (a DnsProvider) SetDomainRecord(domain, ip string) error {
records, _, e := a.Api.ListDNSRecords(context.Background(), a.ZoneResource, cloudflare.ListDNSRecordsParams{
records, _, err := a.Api.ListDNSRecords(context.Background(), a.ZoneResource, cloudflare.ListDNSRecordsParams{
Type: "A",
Name: domain,
})
if e != nil {
return e
if err != nil {
return err
}
if len(records) == 0 {
_, e = a.Api.CreateDNSRecord(context.Background(), a.ZoneResource, cloudflare.CreateDNSRecordParams{
_, err = a.Api.CreateDNSRecord(context.Background(), a.ZoneResource, cloudflare.CreateDNSRecordParams{
Type: "A",
Name: domain,
Content: ip,
TTL: a.TTL,
})
return e
return err
} else {
record := records[0]
if record.Content == ip {
return nil
}
_, e = a.Api.UpdateDNSRecord(context.Background(), a.ZoneResource, cloudflare.UpdateDNSRecordParams{
_, err = a.Api.UpdateDNSRecord(context.Background(), a.ZoneResource, cloudflare.UpdateDNSRecordParams{
ID: record.ID,
Content: ip,
})
return e
return err
}
}

View File

@@ -1,7 +1,7 @@
package dnspod
import (
dnsUtil2 "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/util"
dnsUtil "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/util"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions"
@@ -19,19 +19,19 @@ type DnsProvider struct {
func New(ttl uint64, conf map[string]interface{}, Http http.RoundTripper) (*DnsProvider, error) {
var p = DnsProvider{TTL: ttl}
e := dnsUtil2.DecodeConfig(conf, &p)
if e != nil {
return nil, e
err := dnsUtil.DecodeConfig(conf, &p)
if err != nil {
return nil, err
}
p.Client, e = dnspod.NewClient(common.NewCredential(p.SecretId, p.SecretKey), regions.Guangzhou, profile.NewClientProfile())
p.Client, err = dnspod.NewClient(common.NewCredential(p.SecretId, p.SecretKey), regions.Guangzhou, profile.NewClientProfile())
p.Client.WithHttpTransport(Http)
return &p, e
return &p, err
}
func (a DnsProvider) SetDomainRecord(domain, ip string) error {
subDomain, rootDomain, e := dnsUtil2.DecodeDomain(domain)
if e != nil {
return e
subDomain, rootDomain, err := dnsUtil.DecodeDomain(domain)
if err != nil {
return err
}
var (
@@ -44,8 +44,8 @@ func (a DnsProvider) SetDomainRecord(domain, ip string) error {
reqRecordList.Domain = &rootDomain
reqRecordList.Subdomain = &subDomain
reqRecordList.Limit = &limit
res, e := a.Client.DescribeRecordList(reqRecordList)
if (e != nil && strings.Contains(e.Error(), dnspod.RESOURCENOTFOUND_NODATAOFRECORD)) || (e == nil && len(res.Response.RecordList) == 0) {
res, err := a.Client.DescribeRecordList(reqRecordList)
if (err != nil && strings.Contains(err.Error(), dnspod.RESOURCENOTFOUND_NODATAOFRECORD)) || (err == nil && len(res.Response.RecordList) == 0) {
reqNewRecord := dnspod.NewCreateRecordRequest()
reqNewRecord.TTL = &a.TTL
reqNewRecord.Domain = &rootDomain
@@ -53,10 +53,10 @@ func (a DnsProvider) SetDomainRecord(domain, ip string) error {
reqNewRecord.RecordLine = &recordLine
reqNewRecord.Value = &ip
reqNewRecord.SubDomain = &subDomain
_, e = a.Client.CreateRecord(reqNewRecord)
return e
} else if e != nil {
return e
_, err = a.Client.CreateRecord(reqNewRecord)
return err
} else if err != nil {
return err
}
reqModifyRecord := dnspod.NewModifyRecordRequest()
@@ -66,6 +66,6 @@ func (a DnsProvider) SetDomainRecord(domain, ip string) error {
reqModifyRecord.RecordId = res.Response.RecordList[0].RecordId
reqModifyRecord.RecordLine = &recordLine
reqModifyRecord.RecordType = &recordType
_, e = a.Client.ModifyRecord(reqModifyRecord)
return e
_, err = a.Client.ModifyRecord(reqModifyRecord)
return err
}

View File

@@ -44,26 +44,26 @@ func (a *Api) request(path string, query map[string]interface{}) (map[string]int
query["callback"] = callback
query["_"] = timestamp
httpTool := tool.NewHttpTool(a.Client)
req, e := httpTool.GenReq("GET", &tool.DoHttpReq{
req, err := httpTool.GenReq("GET", &tool.DoHttpReq{
Url: a.BaseUrl + path,
Query: query,
})
if e != nil {
log.Debugln(e)
return nil, e
if err != nil {
log.Debugln(err)
return nil, err
}
resp, e := httpTool.Client.Do(req)
if e != nil {
log.Debugln(e)
return nil, e
resp, err := httpTool.Client.Do(req)
if err != nil {
log.Debugln(err)
return nil, err
}
defer resp.Body.Close()
data, e := io.ReadAll(resp.Body)
if e != nil {
log.Debugln(e)
return nil, e
data, err := io.ReadAll(resp.Body)
if err != nil {
log.Debugln(err)
return nil, err
}
res := string(data)
@@ -83,9 +83,9 @@ func (a *Api) DetectAcid() (string, error) {
addr := a.BaseUrl
for {
log.Debugln("HTTP GET ", addr)
res, e := a.NoDirect.Get(addr)
if e != nil {
return "", e
res, err := a.NoDirect.Get(addr)
if err != nil {
return "", err
}
_ = res.Body.Close()
loc := res.Header.Get("location")
@@ -97,9 +97,9 @@ func (a *Api) DetectAcid() (string, error) {
}
var u *url.URL
u, e = url.Parse(addr)
if e != nil {
return "", e
u, err = url.Parse(addr)
if err != nil {
return "", err
}
acid := u.Query().Get(`ac_id`)
if acid != "" {

View File

@@ -30,13 +30,13 @@ type Srun struct {
api Api
}
func (c Srun) LoginStatus() (online bool, ip string, e error) {
res, e := c.api.GetUserInfo()
if e != nil {
return false, "", e
func (c Srun) LoginStatus() (online bool, ip string, err error) {
res, err := c.api.GetUserInfo()
if err != nil {
return false, "", err
}
err, ok := res["error"]
errRes, ok := res["error"]
if !ok {
return false, "", ErrResultCannotFound
}
@@ -54,7 +54,7 @@ func (c Srun) LoginStatus() (online bool, ip string, e error) {
inet := strings.HasPrefix(ip, "192.168.") || strings.HasPrefix(ip, "10.") || strings.HasPrefix(ip, "172.")
online = err.(string) == "ok" || !inet
online = errRes.(string) == "ok" || !inet
return
}
@@ -66,9 +66,9 @@ func (c Srun) DoLogin(clientIP string) error {
c.LoginInfo.Form.Username += "@" + c.LoginInfo.Form.UserType
}
res, e := c.api.GetChallenge(c.LoginInfo.Form.Username, clientIP)
if e != nil {
return e
res, err := c.api.GetChallenge(c.LoginInfo.Form.Username, clientIP)
if err != nil {
return err
}
token, ok := res["challenge"]
if !ok {
@@ -79,15 +79,15 @@ func (c Srun) DoLogin(clientIP string) error {
log.Debugln("发送登录请求")
info, e := json.Marshal(map[string]string{
info, err := json.Marshal(map[string]string{
"username": c.LoginInfo.Form.Username,
"password": c.LoginInfo.Form.Password,
"ip": clientIP,
"acid": c.LoginInfo.Meta.Acid,
"enc_ver": c.LoginInfo.Meta.Enc,
})
if e != nil {
return e
if err != nil {
return err
}
EncryptedInfo := "{SRBX1}" + Base64(XEncode(string(info), tokenStr))
Md5Str := Md5(tokenStr)
@@ -99,7 +99,7 @@ func (c Srun) DoLogin(clientIP string) error {
tokenStr + EncryptedInfo,
)
res, e = c.api.Login(
res, err = c.api.Login(
c.LoginInfo.Form.Username,
EncryptedMd5,
c.LoginInfo.Meta.Acid,
@@ -109,8 +109,8 @@ func (c Srun) DoLogin(clientIP string) error {
c.LoginInfo.Meta.N,
c.LoginInfo.Meta.Type,
)
if e != nil {
return e
if err != nil {
return err
}
var result interface{}
result, ok = res["error"]

View File

@@ -14,16 +14,16 @@ type Eth struct {
// ConvertInterface 当没有 ipv4 地址时 eth 可能为 nil
func ConvertInterface(eth net.Interface) (*Eth, error) {
addresses, e := eth.Addrs()
if e != nil {
return nil, e
addresses, err := eth.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addresses {
if strings.Contains(addr.String(), ".") {
var ip *net.TCPAddr
ip, e = net.ResolveTCPAddr("tcp", strings.Split(addr.String(), "/")[0]+":0")
if e != nil {
log.Warnln(eth.Name+" ip解析失败", e)
ip, err = net.ResolveTCPAddr("tcp", strings.Split(addr.String(), "/")[0]+":0")
if err != nil {
log.Warnln(eth.Name+" ip解析失败", err)
continue
}
return &Eth{
@@ -38,19 +38,19 @@ func ConvertInterface(eth net.Interface) (*Eth, error) {
func GetInterfaceAddr(regexpStr string) ([]Eth, error) {
var result []Eth
interfaces, e := net.Interfaces()
if e != nil {
return nil, e
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
reg, e := regexp.Compile(regexpStr)
if e != nil {
log.Fatalln("interfaces设置异常无法解析: ", e)
reg, err := regexp.Compile(regexpStr)
if err != nil {
log.Fatalln("interfaces设置异常无法解析: ", err)
}
for _, eth := range interfaces {
if reg.Match([]byte(eth.Name)) {
cEth, e := ConvertInterface(eth)
if e != nil {
log.Warnln(eth.Name+" 网卡地址获取失败: ", e)
cEth, err := ConvertInterface(eth)
if err != nil {
log.Warnln(eth.Name+" 网卡地址获取失败: ", err)
continue
}