forked from sleeyax/aternos-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaternos_api.go
62 lines (55 loc) · 1.58 KB
/
aternos_api.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
package aternos_api
import (
tls "github.com/refraction-networking/utls"
"github.com/sleeyax/aternos-api/internal/tlsadapter"
"github.com/sleeyax/gotcha"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
)
type Api struct {
Options *Options
client *gotcha.Client
// ajax security token.
sec string
// ajax token.
token string
}
// New allocates a new Aternos API instance.
func New(options *Options) *Api {
jar, _ := cookiejar.New(&cookiejar.Options{})
adapter := tlsadapter.New(&tls.Config{ServerName: "aternos.org", InsecureSkipVerify: options.InsecureSkipVerify})
client, _ := gotcha.NewClient(&gotcha.Options{
Adapter: adapter,
PrefixURL: "https://aternos.org/",
Headers: http.Header{
"User-Agent": {"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"},
"Accept": {"*/*"},
"Accept-Language": {"en-US,en;q=0.9"},
},
CookieJar: jar,
FollowRedirect: false,
Retry: false,
Hooks: gotcha.Hooks{
AfterResponse: []gotcha.AfterResponseHook{
func(response *gotcha.Response, retry gotcha.RetryFunc) (*gotcha.Response, error) {
if location := response.Header.Get("location"); strings.Contains(location, "go") {
return response, UnauthenticatedError
}
if response.StatusCode == http.StatusForbidden {
return response, ForbiddenError
}
return response, nil
},
},
},
Proxy: options.Proxy,
})
u, _ := url.Parse(client.Options.PrefixURL)
jar.SetCookies(u, options.Cookies)
return &Api{
Options: options,
client: client,
}
}