-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.py
121 lines (99 loc) · 3.94 KB
/
auth.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# !/usr/bin/python
# -*- coding: utf-8 -*-
# MIT License
#
# Copyright (c) 2021 ITyouG(George Guan)
# https://github.com/ITyouG/Fortinet-Auth-Login
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ver 1.0.1
import time
import requests
from requests import post, head, Session, ConnectionError
from os import path
import sys
googleUrl = "http://www.google.com/"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}
fgt_keepalive_url = "";
class FirewallState:
Fail, Login, BadAuth, Retry, Keepalive, Error = list(range(6))
def login_func(username, password):
print("do login.")
try:
# head(googleUrl)
session = Session()
res = session.get(googleUrl, headers=headers, timeout=3)
print("http_Status_Code:", res.status_code)
if res.history[0].status_code != 303:
print('Already connected. :)')
return FirewallState.Login
else:
magic = res.url.split('?')[1]
payload = {
'4Tredir': 'http://google.com/',
'magic': str(magic),
'username': username,
'password': password,
}
url_2 = 'http://192.168.201.6:1000/'
res = post(url_2, headers=headers, data=payload, timeout=3)
global fgt_keepalive_url
fgt_keepalive_url = res.url
print(fgt_keepalive_url)
return FirewallState.Keepalive
except ConnectionError:
print('ConnectionError - login_func. :(')
return FirewallState.Fail
except:
print("UnexpectedError - login_func. :", sys.exc_info()[0])
return FirewallState.Fail
def keepalive_func():
print("do keepalive.")
try:
response = requests.request("GET", fgt_keepalive_url)
if response.status_code == 200:
print('Already keepalive. :)')
return FirewallState.Keepalive
except ConnectionError:
print('ConnectionError - keepalive_func. :(')
return FirewallState.Fail
except:
print("UnexpectedError - login_func. :", sys.exc_info()[0])
return FirewallState.Fail
def main():
try:
print("init auth.")
# read authcred.
fn = path.expanduser('~/.helloauthcred') # filename
with open(fn, 'r') as f:
(username, password) = [x.strip() for x in f]
state = FirewallState.Login
while True:
if state == FirewallState.Keepalive:
state = keepalive_func()
time.sleep(10)
else:
state = login_func(username, password)
time.sleep(10)
print("state:", state)
except:
print("UnexpectedError - main. :", sys.exc_info()[0])
main()
if __name__ == "__main__":
main()