forked from izlyforever/cf-tool
/
client.go
86 lines (76 loc) · 1.92 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package client
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/fatih/color"
"github.com/magic3007/cf-tool/cookiejar"
)
// Client codeforces client
type Client struct {
Jar *cookiejar.Jar `json:"cookies"`
Handle string `json:"handle"`
HandleOrEmail string `json:"handle_or_email"`
Password string `json:"password"`
Ftaa string `json:"ftaa"`
Bfaa string `json:"bfaa"`
LastSubmission *Info `json:"last_submission"`
host string
proxy string
path string
client *http.Client
}
// Instance global client
var Instance *Client
// Init initialize
func Init(path, host, proxy string) {
jar, _ := cookiejar.New(nil)
c := &Client{Jar: jar, LastSubmission: nil, path: path, host: host, proxy: proxy, client: nil}
if err := c.load(); err != nil {
color.Red(err.Error())
color.Green("Create a new session in %v", path)
}
Proxy := http.ProxyFromEnvironment
if len(proxy) > 0 {
proxyURL, err := url.Parse(proxy)
if err != nil {
color.Red(err.Error())
color.Green("Use default proxy from environment")
} else {
Proxy = http.ProxyURL(proxyURL)
}
}
c.client = &http.Client{Jar: c.Jar, Transport: &http.Transport{Proxy: Proxy}}
if err := c.save(); err != nil {
color.Red(err.Error())
}
Instance = c
}
// load from path
func (c *Client) load() (err error) {
file, err := os.Open(c.path)
if err != nil {
return
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return err
}
return json.Unmarshal(bytes, c)
}
// save file to path
func (c *Client) save() (err error) {
data, err := json.MarshalIndent(c, "", " ")
if err == nil {
os.MkdirAll(filepath.Dir(c.path), os.ModePerm)
err = ioutil.WriteFile(c.path, data, 0644)
}
if err != nil {
color.Red("Cannot save session to %v\n%v", c.path, err.Error())
}
return
}