forked from MatthiasLohr/docker-f5fpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf5fpc-client.py
executable file
·157 lines (128 loc) · 5.4 KB
/
f5fpc-client.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
import argparse
import binascii
import docker
import getpass
import logging
import os
import shlex
import signal
import subprocess
import sys
from distutils.spawn import find_executable
# initialize logging
logging.basicConfig(format='%(asctime)s %(levelname)s (%(name)s) %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# initialize docker
docker_client = docker.from_env()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('host', help='VPN host')
parser.add_argument('user', help='VPN user name')
parser.add_argument('-n', '--network', nargs='*', dest='networks', help='Networks available via VPN in CIDR notation'
' (e.g. 10.0.0.0/8). Will try to set routes'
' (requires root privileges).')
parser.add_argument('--debug', action='store_true')
args = parser.parse_args()
if args.debug:
logger.setLevel(logging.DEBUG)
if os.getuid() != 0 and args.networks and len(args.networks) > 0:
logging.warn('We need root privileges to set the network routes for you!')
container_name = get_container_name(args.host)
password = str(getpass.getpass('Enter your VPN password: '))
# remove old container instance
try:
container = docker_client.containers.get(container_name)
container.remove(force=True)
container = None
logging.debug('Removed old container instance')
except docker.errors.NotFound:
pass
logging.debug('Preparing docker container...')
container = docker_client.containers.run(
'matthiaslohr/f5fpc',
'/opt/idle.sh',
privileged=True,
name=container_name,
detach=True,
environment={
'HOST': args.host,
'USER': args.user,
'HEXPASSWORD': binascii.hexlify(password.encode('utf8'))
}
)
logging.debug('Docker container initialized')
logging.info('Connecting to {host}...'.format(host=args.host))
container_exec(container_name, '/opt/connect.sh')
last_status = -255
while True:
status, stdout, stderr = container_exec(container_name, '/usr/local/bin/f5fpc -i')
logging.debug('f5fpc --info returns {code}'.format(code=status))
if status == 1 and last_status != 1:
logging.info('Session initialized.')
elif status == 2 and last_status != 2:
logging.info('Logging in...')
elif status == 5:
logging.info('Connection established. Welcome to {host} network!'.format(host=args.host))
break
elif status == 7:
logging.error('Login was denied.')
container.stop()
container.remove()
return 1
last_status = status
inspection_result = docker.APIClient().inspect_container(container_name)
container_ip = inspection_result['NetworkSettings']['IPAddress']
logging.debug('Container IP: {ip}'.format(ip=container_ip))
if args.networks:
for network in args.networks:
rc, stdout, stderr = route_add(network, container_ip)
if rc == 0:
logging.debug('Route {net} via {gw} added'.format(net=network, gw=container_ip))
else:
logging.error('Could not add route {net} via {gw}: {reason}'.format(
net=network, gw=container_ip, reason=stderr.strip())
)
if len(args.networks) > 0:
logging.info('Routes added')
# wait for signal
def shutdown(signal, frame):
logging.info('Shutting down...')
if args.networks:
for network in args.networks:
rc, stdout, stderr = route_del(network, container_ip)
if rc == 0:
logging.debug('Route {net} via {gw} removed'.format(net=network, gw=container_ip))
else:
logging.error('Could not delete route {net} via {gw}: {reason}'.format(
net=network, gw=container_ip,reason=stderr.strip())
)
container.stop()
container.remove()
return 0
signal.signal(signal.SIGINT, shutdown)
# TODO monitor connection state!
signal.pause()
def get_container_name(host):
return 'f5fpc_'+host
def container_exec(container, command):
command_string = '{docker} exec -it {container} {command}'.format(docker=find_executable("docker"), container=container, command=command)
command_splitted = shlex.split(command_string)
logger.debug(command_splitted)
process = subprocess.Popen(command_splitted, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
returncode = process.returncode
return returncode, stdout, stderr
def os_exec(command):
command_splitted = shlex.split(command)
process = subprocess.Popen(command_splitted, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
returncode = process.returncode
return returncode, stdout, stderr
def route_add(network, gateway):
return os_exec('ip route add {network} via {gateway}'.format(network=network, gateway=gateway))
def route_del(network, gateway):
return os_exec('ip route del {network} via {gateway}'.format(network=network, gateway=gateway))
if __name__ == '__main__':
sys.exit(main())