示例#1
0
func newAccount() *Account {
	account := &Account{}
	util.ProjectName = "gotrade"
	err := util.YamlFileDecode(util.GetBasePath()+"/config/trade.yaml", account)
	if err != nil {
		panic(err)
	}
	account.Login()
	return account
}
示例#2
0
文件: trader.go 项目: imbugs/gotrade
// 登录
func (account *Account) Login() (err error) {
	cookieJar, _ := cookiejar.New(nil)
	account.logger = util.NewLogger("trader")
	transport := &httpclient.Transport{
		ConnectTimeout:        3 * time.Second,
		RequestTimeout:        3 * time.Second,
		ResponseHeaderTimeout: 3 * time.Second,
	}
	defer transport.Close()
	account.client = &http.Client{
		CheckRedirect: nil,
		Jar:           cookieJar,
		Transport:     transport,
	}
	account.logger.Info("begin login")
	account.baseUrl = "https://tradegw.htsc.com.cn/?"
	cacheByte, _ := ioutil.ReadFile(util.GetBasePath() + "/cache/" + account.Username + "Uid")
	cacheUid := string(cacheByte)
	if cacheUid != "" {
		account.logger.Info("read cache uid : " + cacheUid)
		account.Uid = cacheUid
		_, checkErr := account.Position()
		if checkErr == nil {
			return
		}
	}
	account.logger.Info("get verfiy code")
	loginUrl := "https://service.htsc.com.cn/service/login.jsp"
	req, _ := http.NewRequest("GET", loginUrl, nil)
	resp, _ := account.client.Do(req)
	req, _ = http.NewRequest("GET", "https://service.htsc.com.cn/service/pic/verifyCodeImage.jsp", nil)
	resp, _ = account.client.Do(req)
	defer resp.Body.Close()
	image, _ := ioutil.ReadAll(resp.Body)
	ioutil.WriteFile(util.GetBasePath()+"/cache/verify.jpg", image, 0644)
	var code string
	fmt.Println("input code:")
	fmt.Scanf("%s\n", &code)
	var raw = fmt.Sprintf("userType=jy&loginEvent=1&trdpwdEns=%s&macaddr=08-00-27-CE-7E-3E&hddInfo=VB0088e34c-9198b670+&lipInfo=10.0.2.15+&topath=null&accountType=1&userName=%s&servicePwd=%s&trdpwd=%s&vcode=", account.Password1, account.Username, account.Password2, account.Password1)
	account.logger.Infof("login post code : %s raw : %s", code, raw)
	req, _ = http.NewRequest("POST", "https://service.htsc.com.cn/service/loginAction.do?method=login", strings.NewReader(raw+code))
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("Refer", "https://service.htsc.com.cn/service/login.jsp?logout=yes")
	req.Header.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)")
	os.Remove(util.GetBasePath() + "/cache/verify.jpg")
	resp, _ = account.client.Do(req)
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	account.logger.Info("try to get uid")
	req, _ = http.NewRequest("GET", "https://service.htsc.com.cn/service/flashbusiness_new3.jsp?etfCode=", nil)
	resp, _ = account.client.Do(req)
	body, _ = ioutil.ReadAll(resp.Body)
	re := regexp.MustCompile(`var\ data\ =\ "(.+?)"`)
	result := re.FindAllStringSubmatch(string(body), 1)
	data, _ := base64.StdEncoding.DecodeString(result[0][1])
	type User struct {
		Uid string `json:"uid"`
	}
	user := User{}
	json.Unmarshal([]byte(data), &user)
	account.Uid = user.Uid
	account.logger.Info("get uid success" + user.Uid)
	if user.Uid == "" {
		account.logger.Error("login error")
		return errors.New("login error")
	}
	ioutil.WriteFile(util.GetBasePath()+"/cache/"+account.Username+"Uid", []byte(user.Uid), 0644)
	return
}
示例#3
0
文件: trader.go 项目: imbugs/gotrade
func (account *Account) clearCache() {
	os.Remove(util.GetBasePath() + "/cache/" + account.Username + "Uid")
}