Skip to content

Instantly share code, notes, and snippets.

@ego008
Forked from wjchen/checkin.go
Created April 19, 2017 07:17
Show Gist options
  • Save ego008/ddaabc480bfaac126ba460eb2cd2b636 to your computer and use it in GitHub Desktop.
Save ego008/ddaabc480bfaac126ba460eb2cd2b636 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"compress/flate"
"compress/gzip"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"regexp"
"strings"
)
const user_agent_iphone = "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5"
const user_agent_chrome = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
var cookieJar = &myCookieJar{}
func main() {
kindle1000("username", "pass")
v2ex("username", "pass")
pujia("username", "pass")
}
func pujia(username, password string) {
homeUrl := "https://www.pujia8.com/"
loginUrl := "https://www.pujia8.com/account/login/"
missionUrl := "https://www.pujia8.com/checkin/"
//get csrf
body, err := HttpRequest("GET", loginUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Host": "www.pujia8.com",
"Accept-Encoding": "gzip, deflate",
}, nil)
checkError(err)
re := regexp.MustCompile(`<input type='hidden' name='csrfmiddlewaretoken' value='(.*)'`)
matched := re.FindStringSubmatch(string(body))
if len(matched) < 2 {
fmt.Println("csrfmiddlewaretoken not found")
return
}
//login
bound := "----WebKitFormBoundary4qOi3NDwQwBQCZes"
content_type := fmt.Sprintf("multipart/form-data; boundary=%s", bound)
form_data := multipartForm(map[string]string{
"csrfmiddlewaretoken": matched[1],
"username": username,
"password": password,
"next": "/",
}, bound)
_, err = HttpRequest("POST", loginUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Host": "www.pujia8.com",
"Referer": loginUrl,
"Content-Type": content_type,
"Accept-Encoding": "gzip, deflate",
}, form_data)
checkError(err)
//get home and check login status
body, err = HttpRequest("GET", homeUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Host": "www.pujia8.com",
"Accept-Encoding": "gzip, deflate",
}, nil)
checkError(err)
if strings.Index(string(body), username) > 0 {
//post checkin
body, err = HttpRequest("POST", missionUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Host": "www.pujia8.com",
"Accept-Encoding": "gzip, deflate",
}, nil)
checkError(err)
resp := struct {
Msg string `json:"msg"`
State interface{} `json:"state"`
Errors interface{} `json:"errors"`
Success interface{} `json:"success"`
}{}
err = json.Unmarshal(body, &resp)
checkError(err)
fmt.Println("pujia:", resp.Msg)
} else {
fmt.Println("pujia login error")
return
}
}
func kindle1000(username, password string) {
md := md5.Sum([]byte(password))
password = hex.EncodeToString(md[:])
homeUrl := "http://www.kindle10000.com/forum.php"
missionUrl := "http://www.kindle10000.com/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&sign_as=1&inajax=1"
loginUrl := "http://www.kindle10000.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1"
//login
_, err := HttpRequest("POST", loginUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Host": "www.kindle10000.com",
"Referer": loginUrl,
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding": "gzip, deflate",
}, map[string]string{
"username": username,
"password": password,
"quickforward": "yes",
"handlekey": "ls",
})
checkError(err)
//check login status
body, err := HttpRequest("GET", homeUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Host": "www.kindle1000.com",
"Accept-Encoding": "gzip, deflate",
}, nil)
checkError(err)
formhash := ""
if strings.Index(string(body), username) > 0 {
re := regexp.MustCompile(`formhash=([0-9a-zA-Z]*)`)
matched := re.FindStringSubmatch(string(body))
///fmt.Println(matched[1])
if len(matched) >= 2 {
formhash = matched[1]
}
} else {
fmt.Println("login error")
return
}
//post mission data
body, err = HttpRequest("POST", missionUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Host": "www.kindle10000.com",
"Referer": homeUrl,
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding": "gzip, deflate",
}, map[string]string{
"formhash": formhash,
"qdxq": "yl",
"qdmode": "3",
"todaysay": "",
"fastreply": "0",
})
checkError(err)
msg1 := "您今日已经签到"
msg2 := "成功"
bodyStr := string(body)
if strings.Index(bodyStr, msg1) >= 0 || strings.Index(bodyStr, msg2) >= 0 {
fmt.Println("kindle10000 checkin ok")
} else {
fmt.Println("kindle10000 checkin error:")
//fmt.Println(bodyStr)
}
}
func v2ex(username, password string) {
var loginUrl = "http://v2ex.com/signin"
var missionUrl = "http://v2ex.com/mission/daily"
//get once
body, err := HttpRequest("GET", loginUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Accept-Encoding": "gzip, deflate",
}, nil)
checkError(err)
//login use once user pass
re := regexp.MustCompile(`<input type="hidden" value="(\d+)" name="once" />`)
matched := re.FindStringSubmatch(string(body))
if len(matched) < 2 {
fmt.Println("v2ex: once for v2ex not found")
return
}
_, err = HttpRequest("POST", loginUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Host": "v2ex.com",
"Origin": "http://v2ex.com",
"Referer": loginUrl,
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding": "gzip, deflate",
}, map[string]string{
"next": "/",
"u": username,
"p": password,
"once": matched[1],
})
checkError(err)
//get content of mission url
body, err = HttpRequest("GET", missionUrl, map[string]string{
"User-Agent": user_agent_chrome,
"Accept-Encoding": "gzip, deflate",
}, nil)
checkError(err)
missionHtmlStr := string(body)
if strings.Index(missionHtmlStr, "每日登录奖励已领取") > 0 {
fmt.Println("v2ex: 每日登录奖励已领取")
} else {
//do daily mission
re = regexp.MustCompile(`location.href = '(.*)'`)
matched = re.FindStringSubmatch(missionHtmlStr)
if len(matched) < 2 {
fmt.Println("v2ex: login failed")
return
}
body, err := HttpRequest("GET", "http://www.v2ex.com"+matched[1], map[string]string{
"User-Agent": user_agent_chrome,
"Accept-Encoding": "gzip, deflate",
}, nil)
checkError(err)
missionHtmlStr := string(body)
if strings.Index(missionHtmlStr, "每日登录奖励已领取") > 0 {
fmt.Println("v2ex: 每日登录奖励已领取")
} else {
fmt.Println("v2ex: checkin failed")
}
}
}
const MAX_BODY_SIZE = 8 * 1024 * 1024 //2M
func HttpRequest(method, url_str string, head_map, form_map interface{}) ([]byte, error) {
buffer := bytes.NewBuffer(nil)
url_obj, err := url.Parse(url_str)
if err != nil {
return buffer.Bytes(), errors.New("Url format error")
}
client := &http.Client{Jar: cookieJar}
var dataBody io.Reader
if form_map != nil {
switch v := form_map.(type) {
case map[string]string:
form_value := url.Values{}
for key, value := range v {
form_value.Set(key, value)
}
dataBody = strings.NewReader(form_value.Encode())
case string:
dataBody = strings.NewReader(v)
case []byte:
dataBody = bytes.NewReader(v)
default:
return buffer.Bytes(), errors.New("The form arg is not a map")
}
}
request, err := http.NewRequest(method, url_obj.String(), dataBody)
if err != nil {
return buffer.Bytes(), errors.New("HTTP GET failed")
}
if head_map != nil {
switch v := head_map.(type) {
case map[string]string:
for key, value := range v {
request.Header.Add(key, value)
}
default:
return buffer.Bytes(), errors.New("The head arg is not a map")
}
}
for _, c := range cookieJar.cookies {
request.AddCookie(c)
}
response, err := client.Do(request)
if err != nil {
return buffer.Bytes(), errors.New("HTTP GET failed")
}
defer response.Body.Close()
if response.StatusCode != 200 {
return buffer.Bytes(), errors.New(fmt.Sprintf("HTTP GET failed, status code %d", response.StatusCode))
}
buf := make([]byte, 4096)
var rd interface{}
switch response.Header.Get("Content-Encoding") {
case "gzip":
var err error
rd, err = gzip.NewReader(response.Body)
if err != nil {
return buffer.Bytes(), errors.New("HTTP body read error")
}
case "deflate":
rd = flate.NewReader(response.Body)
default:
rd = bufio.NewReader(response.Body)
}
for {
var n int
var err error
switch v := rd.(type) {
case *gzip.Reader:
n, err = v.Read(buf)
case io.ReadCloser:
n, err = v.Read(buf)
case *bufio.Reader:
n, err = v.Read(buf)
}
if err != nil && err != io.EOF {
return buffer.Bytes(), errors.New("HTTP body read error")
}
if n == 0 {
break
}
buffer.Write(buf[:n])
if buffer.Len() > MAX_BODY_SIZE {
return buffer.Bytes(), errors.New("HTTP body too larg, partial readed")
}
}
return buffer.Bytes(), nil
}
func multipartForm(form_map map[string]string, boundary string) []byte {
buf := bytes.Buffer{}
crlf := []byte{0x0d, 0x0a}
disFmt := `Content-Disposition: form-data; name="%s"`
for key, value := range form_map {
buf.WriteString("--")
buf.WriteString(boundary)
buf.Write(crlf)
keyStr := fmt.Sprintf(disFmt, key)
buf.WriteString(keyStr)
buf.Write(crlf)
buf.Write(crlf)
buf.WriteString(value)
buf.Write(crlf)
}
buf.WriteString("--")
buf.WriteString(boundary)
buf.WriteString("--") //end
buf.Write(crlf)
return buf.Bytes()
}
type myCookieJar struct {
cookies []*http.Cookie
}
func (c *myCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
if c.cookies == nil {
c.cookies = make([]*http.Cookie, 0)
}
for _, it := range cookies {
c.cookies = append(c.cookies, it)
}
}
func (c *myCookieJar) Cookies(u *url.URL) []*http.Cookie {
return c.cookies
}
func checkError(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment