使用Go來實現(xiàn)網(wǎng)游的簽到
本篇文章啤誊,實際應(yīng)用中并沒有多大意義。不適用于所有網(wǎng)友及網(wǎng)游笤昨。只是筆者寓教于樂的一種形式捆憎。
主要是學(xué)習(xí)和練習(xí)Go語言的以下知識點:
- 變量的創(chuàng)建和賦值
- 數(shù)組的創(chuàng)建、賦值和遍歷
- map的創(chuàng)建迟隅、修改和遍歷
- MD5加密
- 網(wǎng)絡(luò)請求
- 打印
[TOC]
卡巴拉島 暢玩服
簽到功能用的是網(wǎng)頁實現(xiàn)但骨。
地址:
[POST]:http://120.78.201.23:808/cShop/item_checkpt.asp
[POST]:http://120.78.201.23:808/cShop/gacha.asp
需要在Body中攜帶參數(shù)
id=test1314
key=e13efe12345656e3
world=1
其中:id是用戶的賬號名稱,key是密碼的16位小寫MD5智袭,world是服務(wù)器id奔缠。
id=test1314&key=e13efe12345656e3&world=1
簡單實現(xiàn)
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main() {
sendRequest4()
}
func sendRequest4() {
// Request (4) (POST http://120.78.201.23:808/cShop/gacha.asp)
params := url.Values{}
params.Set("id", "這里替換成自己的賬號")
params.Set("key", "這里替換成自己密碼的小寫16位MD5")
params.Set("world", "1")
body := bytes.NewBufferString(params.Encode())
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest("POST", "http://120.78.201.23:808/cShop/gacha.asp", body)
// Headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)
// Display Results
fmt.Println("response Status : ", resp.Status)
fmt.Println("response Headers : ", resp.Header)
fmt.Println("response Body : ", string(respBody))
}
變型封裝
將網(wǎng)絡(luò)調(diào)用,抽象封裝补履。把賬號名和密碼作為傳參傳遞進功能函數(shù)添坊。
暫不考慮密碼加密的問題,即已知加密后的密碼箫锤。
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main() {
checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
id := "這里替換成自己的賬號"
key := "這里替換成自己密碼的小寫16位MD5"
sendRequest(checkptUrl,id,key)
}
func sendRequest(_url string,_id string,_key string) {
params := url.Values{}
params.Set("id", _id)
params.Set("key", _key)
params.Set("world", "1")
body := bytes.NewBufferString(params.Encode())
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest("POST", _url, body)
// Headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)
// Display Results
fmt.Println("response Status : ", resp.Status)
fmt.Println("response Body : ", string(respBody))
}
實現(xiàn)MD5加密
實現(xiàn)32位小寫的MD5
func md5(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum([]byte(nil)))
}
實現(xiàn)16位小寫的MD5贬蛙。就是把標準普通的MD5字符串從中截取了。
func md5_16(str string) string {
h := md5.New()
h.Write([]byte(str))
rs :=[]rune(hex.EncodeToString(h.Sum([]byte(nil))))
return string(rs[8:24])
}
進一步改寫
通過用戶名和密碼(明文)進行功能的運行
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main() {
checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
id := "這里替換成自己的賬號"
key := "這里替換成自己的密碼"
sendRequest(checkptUrl, id, md5_16(key))
}
func md5_16(str string) string {
h := md5.New()
h.Write([]byte(str))
rs := []rune(hex.EncodeToString(h.Sum([]byte(nil))))
return string(rs[8:24])
}
func sendRequest(_url string, _id string, _key string) {
// Request (POST http://120.78.201.23:808/cShop/gacha.asp)
params := url.Values{}
params.Set("id", _id)
params.Set("key", _key)
params.Set("world", "1")
body := bytes.NewBufferString(params.Encode())
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest("POST", _url, body)
// Headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)
// Display Results
fmt.Println("response Status : ", resp.Status)
fmt.Println("response Body : ", string(respBody))
}
多賬號
使用數(shù)組來實現(xiàn)多賬號列表谚攒。
數(shù)組實現(xiàn)
這里暫時沒有對密碼數(shù)組和賬號數(shù)組的長度是否一直做判定阳准,默認為長度相同。
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main() {
ids := [...]string{"賬號1", "賬號2", "賬號3"}
keys := [...]string{"賬號1密碼", "賬號2密碼", "賬號3密碼"}
checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
//gachaUrl := "http://120.78.201.23:808/cShop/gacha.asp"
for index := 0; index < len(ids); index++ {
sendRequest(checkptUrl, ids[index], md5_16(keys[index]))
}
}
func sendRequest(_url string, _id string, _key string) {
// Request (POST http://120.78.201.23:808/cShop/gacha.asp)
params := url.Values{}
params.Set("id", _id)
params.Set("key", _key)
params.Set("world", "1")
body := bytes.NewBufferString(params.Encode())
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest("POST", _url, body)
// Headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)
// Display Results
fmt.Println("response Status : ", resp.Status)
//fmt.Println("response Headers : ", resp.Header)
fmt.Println("response Body : ", string(respBody))
}
func md5_16(str string) string {
h := md5.New()
h.Write([]byte(str))
rs := []rune(hex.EncodeToString(h.Sum([]byte(nil))))
return string(rs[8:24])
}
賬號密碼相同
假如用戶名和密碼是相同的特殊情況馏臭。
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main() {
ids := [...]string{"賬號1", "賬號2", "賬號3"}
checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
//gachaUrl := "http://120.78.201.23:808/cShop/gacha.asp"
for _,value := range ids {
sendRequest(checkptUrl,value,md5_16(value))
}
}
func sendRequest(_url string,_id string,_key string) {
// Request (POST http://120.78.201.23:808/cShop/gacha.asp)
params := url.Values{}
params.Set("id", _id)
params.Set("key", _key)
params.Set("world", "1")
body := bytes.NewBufferString(params.Encode())
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest("POST", _url, body)
// Headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)
// Display Results
fmt.Println("response Status : ", resp.Status)
//fmt.Println("response Headers : ", resp.Header)
fmt.Println("response Body : ", string(respBody))
}
func md5_16(str string) string {
h := md5.New()
h.Write([]byte(str))
rs :=[]rune(hex.EncodeToString(h.Sum([]byte(nil))))
return string(rs[8:24])
}
map實現(xiàn)
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main() {
checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
//gachaUrl := "http://120.78.201.23:808/cShop/gacha.asp"
map_id := make(map[string]string)
map_id["賬號1"] = md5_16("密碼1")
map_id["賬號2"] = md5_16("密碼2")
map_id["賬號3"] = md5_16("密碼3")
for key,value := range ids {
sendRequest(checkptUrl,key,value)
}
}
func sendRequest(_url string,_id string,_key string) {
// Request (POST http://120.78.201.23:808/cShop/gacha.asp)
params := url.Values{}
params.Set("id", _id)
params.Set("key", _key)
params.Set("world", "1")
body := bytes.NewBufferString(params.Encode())
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest("POST", _url, body)
// Headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)
// Display Results
fmt.Println("response Status : ", resp.Status)
//fmt.Println("response Headers : ", resp.Header)
fmt.Println("response Body : ", string(respBody))
}
func md5_16(str string) string {
h := md5.New()
h.Write([]byte(str))
rs :=[]rune(hex.EncodeToString(h.Sum([]byte(nil))))
return string(rs[8:24])
}