281 lines
9.7 KiB
Go
281 lines
9.7 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"go-account-register/libs"
|
|
"go-account-register/models"
|
|
"go-account-register/utils"
|
|
"log"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-rod/rod"
|
|
"github.com/go-rod/rod/lib/cdp"
|
|
"github.com/go-rod/rod/lib/proto"
|
|
)
|
|
|
|
type TwitterService struct{}
|
|
|
|
// 登录
|
|
func (*TwitterService) Login(id int) *libs.ErrorInfo {
|
|
UserService := InitTwitterAccountService()
|
|
user := UserService.GetInfo(int64(id))
|
|
obj := Browser.GetBrowser(id, &libs.Fingerprint{
|
|
Proxy: user.Proxy,
|
|
})
|
|
browser := obj.Browser
|
|
page := obj.Page
|
|
|
|
err3 := rod.Try(func() {
|
|
// 创建 Cookie 对象
|
|
// cookies := user.Cookies
|
|
// log.Println("获取cookie", cookies)
|
|
// b, _ := json.MarshalIndent(cookies, "", " ")
|
|
// log.Println(string(b))
|
|
var token string
|
|
for _, v := range user.Cookies {
|
|
if v.Name == "auth_token" {
|
|
token = v.Value
|
|
}
|
|
}
|
|
log.Println("当前账号Token", token)
|
|
cookies := []*proto.NetworkCookieParam{
|
|
{
|
|
Name: "auth_token",
|
|
Value: token,
|
|
Domain: ".x.com",
|
|
Path: "/",
|
|
Expires: proto.TimeSinceEpoch(time.Now().Add(24 * time.Hour).Unix()), // 过期时间
|
|
HTTPOnly: true, // 禁止 JS 访问
|
|
Secure: true, // 仅 HTTPS 传输
|
|
},
|
|
}
|
|
// 全局设置
|
|
browser.SetCookies(cookies)
|
|
page.MustNavigate("https://x.com/home").MustWaitLoad()
|
|
page.MustWaitNavigation()
|
|
})
|
|
if e, ok := err3.(*cdp.Error); ok {
|
|
if e.Code == -32000 && e.Message == "Inspected target navigated or closed" {
|
|
Browser.CancelBrowser(id)
|
|
return libs.ErrorCode["LoginFailed"]
|
|
}
|
|
}
|
|
|
|
time.Sleep(3 * time.Second)
|
|
url := page.MustEval("() => window.location.href").String()
|
|
if url == "https://x.com/account/access" {
|
|
Browser.CancelBrowser(id)
|
|
return libs.ErrorCode["AccountLocked"]
|
|
}
|
|
log.Println(url)
|
|
err1 := rod.Try(func() {
|
|
page.Timeout(5 * time.Second).MustElement(`input[autocomplete="username"]`)
|
|
})
|
|
if err1 == nil {
|
|
Browser.CancelBrowser(id)
|
|
return libs.ErrorCode["LoginFailed"]
|
|
}
|
|
// 是否检测到侧边栏
|
|
err2 := rod.Try(func() {
|
|
page.Timeout(5 * time.Second).MustElement(`a[data-testid="AppTabBar_Profile_Link"]`)
|
|
})
|
|
|
|
if err2 != nil {
|
|
Browser.CancelBrowser(id)
|
|
return libs.ErrorCode["LoginFailed"]
|
|
}
|
|
|
|
// 是否检测到帖子列表
|
|
err := rod.Try(func() {
|
|
page.Timeout(5 * time.Second).MustElement(`article[data-testid="tweet"]`)
|
|
})
|
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
fmt.Println("请求空闲等待超时!")
|
|
// 关闭浏览器
|
|
Browser.CancelBrowser(id)
|
|
return libs.ErrorCode["NetworkTimeout"]
|
|
}
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
return libs.ErrorCode["LoginSuccessful"]
|
|
}
|
|
|
|
// 退出登录
|
|
func (*TwitterService) Logout(id int) {
|
|
Browser.CancelBrowser(id)
|
|
}
|
|
|
|
func (bit *TwitterService) Register() *libs.ErrorInfo {
|
|
// 随机获取代理
|
|
proxyService := InitProxyService()
|
|
proxy := proxyService.GetInfo()
|
|
|
|
// 获取一个邮箱
|
|
emailService := InitEmailService()
|
|
|
|
emailInfo := emailService.GetEmailOne()
|
|
log.Println("当前邮箱信息", emailInfo)
|
|
obj := Browser.GetNewBrowser(&libs.Fingerprint{})
|
|
page := obj.Page
|
|
page.MustNavigate("https://x.com/").MustWaitLoad()
|
|
// 点击创建账号
|
|
createAccountA := page.Timeout(5 * time.Second).MustElement(`a[data-testid="signupButton"]`)
|
|
createAccountA.MustClick()
|
|
|
|
// // 点击创建账号
|
|
// createAccount := page.Timeout(5 * time.Second).MustElement(`button.css-175oi2r.r-sdzlij.r-1phboty.r-rs99b7.r-lrvibr.r-ywje51.r-184id4b.r-13qz1uu.r-2yi16.r-1qi8awa.r-3pj75a.r-1loqt21.r-o7ynqc.r-6416eg.r-1ny4l3l`)
|
|
// createAccount.MustClick()
|
|
// 判断当前是否是邮箱注册
|
|
// _, err := page.Element(`input[name="email"]`)
|
|
// if err != nil {
|
|
switchButton := page.Timeout(5 * time.Second).MustElement(`button.css-146c3p1.r-bcqeeo.r-qvutc0.r-37j5jr.r-1ff274t.r-a023e6.r-rjixqe.r-16dba41`)
|
|
switchButton.MustClick()
|
|
// }
|
|
// 获取邮箱输入框
|
|
inputEmail := page.Timeout(5 * time.Second).MustElement(`input[name="email"]`)
|
|
inputEmail.MustFocus()
|
|
inputEmail.MustInput(emailInfo.Email)
|
|
// 获取名称输入框
|
|
inputName := page.Timeout(5 * time.Second).MustElement(`input[name="name"]`)
|
|
inputName.MustFocus()
|
|
nameStr := emailInfo.Name
|
|
if emailInfo.Name == "" {
|
|
nameStr = utils.RandString(12)
|
|
}
|
|
inputName.MustInput(nameStr)
|
|
// 随机生成生日
|
|
dateParams := utils.RandDate(18, 80)
|
|
dateSelects, _ := page.Timeout(5 * time.Second).Elements(`select.r-30o5oe.r-1niwhzg.r-17gur6a.r-1yadl64.r-18jsvk2.r-1loqt21.r-1inkyih.r-rjixqe.r-crgep1.r-1wzrnnt.r-1ny4l3l.r-t60dpp.r-xd6kpl.r-is05cd.r-ttdzmv`)
|
|
log.Println(dateSelects)
|
|
fmt.Println(strconv.Itoa(dateParams.Month))
|
|
fmt.Println(strconv.Itoa(dateParams.Day))
|
|
fmt.Println(strconv.Itoa(dateParams.Year))
|
|
time.Sleep(2 * time.Second)
|
|
// 月份
|
|
dateSelects[0].WaitVisible()
|
|
opts, _ := dateSelects[0].Elements("option")
|
|
for i, opt := range opts {
|
|
txt, _ := opt.Text()
|
|
val, _ := opt.Attribute("value")
|
|
log.Printf("option[%d] text=%s value=%s\n", i, txt, *val)
|
|
if i != 0 && i == dateParams.Month {
|
|
dateSelects[0].MustSelect(txt)
|
|
}
|
|
}
|
|
// 年份
|
|
dateSelects[2].WaitVisible()
|
|
dateSelects[2].MustSelect(strconv.Itoa(dateParams.Year))
|
|
time.Sleep(2 * time.Second)
|
|
// 日期
|
|
dateSelects[1].WaitVisible()
|
|
dateSelects[1].MustSelect(strconv.Itoa(dateParams.Day))
|
|
// dateSelects[0].MustClick()
|
|
// monthOption := dateSelects[0].MustElement(fmt.Sprintf(`option[value="%d"]`, dateParams.Month))
|
|
// monthOption.MustClick()
|
|
time.Sleep(2 * time.Second)
|
|
// 点击下一步
|
|
ocfSignupNextLink := page.Timeout(5 * time.Second).MustElement(`button[data-testid="ocfSignupNextLink"]`)
|
|
ocfSignupNextLink.MustClick()
|
|
// 判断是否需要验证码
|
|
time.Sleep(4 * time.Second)
|
|
_, err1 := page.Timeout(5 * time.Second).Element(`input[name="verfication_code"]`)
|
|
if err1 != nil {
|
|
// res := getApiResponse(page, "")
|
|
}
|
|
//TODO 对接获取邮箱信息接口
|
|
time.Sleep(10 * time.Second)
|
|
codeValue := LoopGetEmailCode("pop.dragonsmail.com:993", emailInfo.Email, emailInfo.Password)
|
|
if codeValue == "0" {
|
|
emailService.Update(emailInfo.Email, models.Email{
|
|
Status: 0,
|
|
})
|
|
return libs.ErrorCode["MailboxUnavailable"]
|
|
}
|
|
// 获取邮箱验证码
|
|
log.Println("获取当前验证码", codeValue)
|
|
codeInput1, _ := page.Timeout(5 * time.Second).Element(`input[name="verfication_code"]`)
|
|
codeInput1.MustInput(codeValue)
|
|
time.Sleep(2 * time.Second)
|
|
// 点击下一步
|
|
codeNextButton := page.Timeout(5 * time.Second).MustElement(`button.css-175oi2r.r-sdzlij.r-1phboty.r-rs99b7.r-lrvibr.r-19yznuf.r-64el8z.r-1fkl15p.r-1loqt21.r-o7ynqc.r-6416eg.r-1ny4l3l`)
|
|
codeNextButton.MustClick()
|
|
// // 点击验证按钮
|
|
// verifyButton := page.Timeout(5 * time.Second).MustElement(`button[data-theme="home.verifyButton"]`)
|
|
// verifyButton.MustClick()
|
|
// 输入密码
|
|
passwordInput, err2 := page.Timeout(5 * time.Second).Element(`input[name="password"]`)
|
|
if err2 != nil {
|
|
// res := getApiResponse(page, "")
|
|
}
|
|
|
|
passwordInput.MustInput(emailInfo.Password)
|
|
time.Sleep(5 * time.Second)
|
|
// 点击下一步
|
|
loginButton := page.Timeout(5 * time.Second).MustElement(`button[data-testid="LoginForm_Login_Button"]`)
|
|
loginButton.MustClick()
|
|
time.Sleep(5 * time.Second)
|
|
page.MustNavigate("https://x.com").MustWaitLoad()
|
|
time.Sleep(10 * time.Second)
|
|
// 获取cookie
|
|
cookieArr := GetPageCookie(page, "https://x.com")
|
|
// 创建用户
|
|
twitterAccountService := InitTwitterAccountService()
|
|
twitterAccountService.Create(&models.TwitterAccount{
|
|
Name: nameStr,
|
|
Email: emailInfo.Email,
|
|
Password: emailInfo.Password,
|
|
Birthday: strconv.Itoa(dateParams.Year) + "-" + strconv.Itoa(dateParams.Month) + "-" + strconv.Itoa(dateParams.Day),
|
|
Cookies: cookieArr,
|
|
Status: "注册成功",
|
|
LoginStatus: "登录成功",
|
|
Proxy: func() string {
|
|
if proxy != nil && proxy.Type != "" {
|
|
return proxy.Type + "://" + proxy.Proxy
|
|
}
|
|
return ""
|
|
}(),
|
|
})
|
|
emailService.Update(emailInfo.Email, models.Email{
|
|
UseStatus: 1,
|
|
})
|
|
// 设置fa2
|
|
page.MustNavigate("https://x.com/settings/account/login_verification").MustWaitLoad()
|
|
// 点击fa2按钮
|
|
inputCheckbox := page.Timeout(5 * time.Second).MustElement(`input[aria-describedby="CHECKBOX_2_LABEL"]`)
|
|
inputCheckbox.MustClick()
|
|
// 等待2秒
|
|
time.Sleep(2 * time.Second)
|
|
// 设置密码
|
|
inpurPassword := page.Timeout(5 * time.Second).MustElement(`input[name="password"]`)
|
|
inpurPassword.MustInput(emailInfo.Password)
|
|
// 点击确认
|
|
faConfirm := page.Timeout(5 * time.Second).MustElement(`button[data-testid="LoginForm_Login_Button"]`)
|
|
faConfirm.MustClick()
|
|
// 等待2秒
|
|
time.Sleep(2 * time.Second)
|
|
ActionListNextButton := page.Timeout(5 * time.Second).MustElement(`button[data-testid="ActionListNextButton"]`)
|
|
ActionListNextButton.MustClick()
|
|
// 点击无法扫描二维码
|
|
queryCode := page.Timeout(5 * time.Second).MustElement("div.css-175oi2r.r-1awozwy.r-w7s2jr>div.css-175oi2r.r-95jzfe>div.css-146c3p1.r-bcqeeo.r-1ttztb7.r-qvutc0.r-37j5jr.r-a023e6.r-rjixqe.r-16dba41>button.css-1jxf684.r-bcqeeo.r-qvutc0.r-poiln3.r-fdjqy7")
|
|
queryCode.MustClick()
|
|
fa2Code := page.Timeout(5 * time.Second).MustElement(`div.css-146c3p1.r-bcqeeo.r-qvutc0.r-37j5jr.r-1blvdjr.r-vrz42v.r-b88u0q.r-x572qd.r-ywje51.r-1oqcu8e.r-q4m81j.r-13qz1uu`)
|
|
fa2CodeText := fa2Code.MustText()
|
|
log.Println("当前账号的二步验证码,", fa2CodeText)
|
|
ocfShowCodeNextLink := page.Timeout(5 * time.Second).MustElement(`button[data-testid="ocfShowCodeNextLink"]`)
|
|
ocfShowCodeNextLink.MustClick()
|
|
codeText, _ := utils.GetFA2Code(fa2CodeText)
|
|
ocfEnterTextTextInput := page.Timeout(5 * time.Second).MustElement(`input[data-testid="ocfEnterTextTextInput"]`)
|
|
ocfEnterTextTextInput.MustInput(codeText)
|
|
ocfEnterTextNextButton := page.Timeout(5 * time.Second).MustElement(`button[data-testid="ocfEnterTextNextButton"]`)
|
|
ocfEnterTextNextButton.MustClick()
|
|
// 删除浏览器
|
|
return libs.ErrorCode["RegisterSuccessful"]
|
|
|
|
}
|