Last active
September 2, 2015 15:25
-
-
Save bigeagle/3361784686e5c61095b7 to your computer and use it in GitHub Desktop.
TUNET connector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"crypto/md5" | |
"crypto/rc4" | |
"encoding/base64" | |
"flag" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
"os" | |
"os/user" | |
"github.com/gcmurphy/getpass" | |
) | |
var auth_file string | |
var toLogout bool | |
const ( | |
BASE_URL = "https://net.tsinghua.edu.cn" | |
LOGIN_URL = BASE_URL + "/do_login.php" | |
LOGOUT_URL = BASE_URL + "/do_login.php" | |
KEY = "tunet3.1415926" | |
) | |
func login(username, password string) error { | |
h := md5.New() | |
io.WriteString(h, password) | |
md5pass := fmt.Sprintf("{MD5_HEX}%x", h.Sum(nil)) | |
form := url.Values{ | |
"username": {username}, | |
"password": {md5pass}, | |
"ac_id": {"1"}, | |
"action": {"login"}, | |
} | |
resp, err := http.PostForm(LOGIN_URL, form) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
fmt.Println(string(body)) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func logout() error { | |
form := url.Values{ | |
"action": {"logout"}, | |
} | |
resp, err := http.PostForm(LOGIN_URL, form) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
fmt.Println(string(body)) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func encrypt(passwd string) string { | |
c, _ := rc4.NewCipher([]byte(KEY)) | |
src := []byte(passwd) | |
dst := make([]byte, len(passwd)) | |
c.XORKeyStream(dst, src) | |
return base64.StdEncoding.EncodeToString(dst) | |
} | |
func decrypt(epasswd string) string { | |
c, _ := rc4.NewCipher([]byte(KEY)) | |
src, _ := base64.StdEncoding.DecodeString(epasswd) | |
dst := make([]byte, len(src)) | |
c.XORKeyStream(dst, src) | |
return string(dst) | |
} | |
func main() { | |
checkError := func(err error) { | |
if err != nil { | |
fmt.Printf("Error: %s\n", err.Error()) | |
os.Exit(1) | |
} | |
} | |
usr, _ := user.Current() | |
flag.StringVar(&auth_file, "a", usr.HomeDir+"/.tunet.passwd", "Username and password file") | |
flag.BoolVar(&toLogout, "k", false, "Logout") | |
flag.Parse() | |
if toLogout { | |
logout() | |
} else { | |
f, err := os.Open(auth_file) | |
if os.IsNotExist(err) { | |
fmt.Println("Auth file does not exist, initalizing") | |
var username, passwd string | |
fmt.Print("Username: ") | |
fmt.Scanf("%s\n", &username) | |
passwd, err := getpass.GetPassConfirm() | |
checkError(err) | |
f2, err := os.Create(auth_file) | |
if err != nil { | |
fmt.Printf("Error: %s\n", err.Error()) | |
fmt.Println("Plase paste following to ", auth_file) | |
fmt.Println("----------------------------------------") | |
fmt.Println(username) | |
fmt.Println(encrypt(passwd)) | |
fmt.Println("----------------------------------------") | |
os.Exit(1) | |
} else { | |
defer f2.Close() | |
_, err = f2.WriteString(username + "\n") | |
checkError(err) | |
_, err = f2.WriteString(encrypt(passwd) + "\n") | |
checkError(err) | |
f2.Sync() | |
login(username, passwd) | |
return | |
} | |
} else { | |
checkError(err) | |
} | |
defer f.Close() | |
rd := bufio.NewReader(f) | |
username, _, err := rd.ReadLine() | |
checkError(err) | |
epasswd, _, err := rd.ReadLine() | |
checkError(err) | |
passwd := decrypt(string(epasswd)) | |
login(string(username), string(passwd)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
USER="username" | |
PASSWORD="password" | |
PASSWORD=`echo -n $PASSWORD|md5sum|cut -d' ' -f1` | |
curl 'https://net.tsinghua.edu.cn/do_login.php' -X 'POST' -H 'Origin: http://net.tsinghua.edu.cn' \ | |
--data "action=login&username=${USER}&password={MD5_HEX}${PASSWORD}&ac_id=1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment