-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.py
executable file
·133 lines (101 loc) · 3.44 KB
/
remote.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import os
import sys
from base64 import b64decode
import random
from ergonomica.lib.interface.prompt import prompt
import string
from ergonomica.lib.lang.environment import Environment
def file_lines(stdin):
split_lines = []
for line in stdin.split("\n"):
if line.startswith("#"):
pass
elif line.startswith(" "):
split_lines[-1] += line
else:
split_lines.append(line)
return split_lines
vowels = list('aeiou')
try:
input = raw_input
except NameError:
pass
def gen_word(min, max):
word = ''
syllables = min + int(random.random() * (max - min))
for i in range(0, syllables):
word += gen_syllable()
return word.capitalize()
def gen_syllable():
ran = random.random()
if ran < 0.333:
return word_part('v') + word_part('c')
if ran < 0.666:
return word_part('c') + word_part('v')
return word_part('c') + word_part('v') + word_part('c')
def word_part(type):
if type is 'c':
return random.sample([ch for ch in list(string.lowercase) if ch not in vowels], 1)[0]
if type is 'v':
return random.sample(vowels, 1)[0]
def gen_valid_word():
return gen_word(2,4)
def keygen():
return os.urandom(16).decode("utf-8")
def sendmsg(ip, port, msg):
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = (ip, port)
sock.connect((ip, port))
recv = ""
try:
sock.sendall(msg)
recv = sock.recv(1000)
finally:
sock.close()
return recv
def connect(ip="0.0.0.0", port=2222):
key = input("[ergo: remote]: Please enter a key: ")
Env = Environment()
Env.prompt = "({}).: ".format(ip)
while True:
stdin = str(prompt(Env, {}))
for line in file_lines(stdin):
print(sendmsg(ip, port, key + line))
def server(port=2222):
from ergonomica.lib.lang.interpreter import ergo_to_string
print("[ergo: remote]: Starting an Ergonomica server locally on 0.0.0.0:{}...".format(str(port)))
key = gen_valid_word() + " " + gen_valid_word() + " " + gen_valid_word()
print("[ergo: remote]: Key is {}.".format(key))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('0.0.0.0', port)
sock.bind(server_address)
sock.listen(1)
while True:
# Wait for a connection
print("[ergo: remote]: Waiting for a connection...")
connection, client_address = sock.accept()
try:
print("[ergo: remote]: Connection from {}.".format(client_address))
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(8192)
if data:
if data.startswith(key):
data = data[len(key):]
print("[ergo: remote]:<{}> {}".format(client_address, data))
connection.sendall(str(ergo_to_string(data)))
else:
print("[ergo: remote]: UNAUTHENTICATED CLIENT.")
else:
print("No more data from client!")
break
finally:
# Clean up the connection
connection.close()
exports = {'connect': connect,
'server': server}