Support IPv4/IPv6 #86

This commit is contained in:
Matteo ℱan 2020-08-03 23:13:45 +02:00
parent 73965b1cdb
commit 5b328d20f4
No known key found for this signature in database
GPG Key ID: 3C30A05BC133D9B6
2 changed files with 25 additions and 6 deletions

View File

@ -21,7 +21,7 @@ _py-kms_ is a port of node-kms created by [cyrozap](http://forums.mydigitallife.
- Microsoft Office 2016 ( Volume License )
- Microsoft Office 2019 ( Volume License )
- It's written in Python:
- tested with Python 3.6.7
- tested with Python 3.6.9
# Dependencies
- Python 3.x.
@ -34,7 +34,7 @@ _py-kms_ is a port of node-kms created by [cyrozap](http://forums.mydigitallife.
- `sudo pip3 install tzlocal pysqlite3`
# Usage
- To start the server, execute `python3 pykms_Server.py [IPADDRESS] [PORT]`, the default _IPADDRESS_ is `::` ( all interfaces ) and the default _PORT_ is `1688`. Note that both the address and port are optional. Also note that you have to use an IPv6 address - even if you are just plan to use IPv4 (the kernel maps the incoming IPv4 requests automatically to IPv6), otherwise you will get unsupported address family exceptions!
- To start the server, execute `python3 pykms_Server.py [IPADDRESS] [PORT]`, the default _IPADDRESS_ is `0.0.0.0` ( all interfaces ) and the default _PORT_ is `1688`. Note that both the address and port are optional. It's allowed to use IPv4 and IPv6 addresses. If you have a IPv6-capable dual-stack OS, a dual-stack socket is created when using a IPv6 address.
- To run the client (only for testing purposes), use `python3 pykms_Client.py [IPADDRESS] [PORT]`, with the same defaults of `pykms_Server.py`.
- To show the help pages type: `python3 pykms_Server.py -h` and `python3 pykms_Client.py -h`.
- To generate a random HWID use `-w` option: `python3 pykms_Server.py -w RANDOM`.

View File

@ -14,6 +14,7 @@ import socketserver
import queue as Queue
import selectors
from time import monotonic as time
import ipaddress
import pykms_RpcBind, pykms_RpcRequest
from pykms_RpcBase import rpcBase
@ -36,9 +37,8 @@ class KeyServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
daemon_threads = True
allow_reuse_address = True
def __init__(self, server_address, RequestHandlerClass):
self.address_family = socket.AF_INET6 # This call make sure the server creates an IPv6 socket and NOT an IPv4 by default
socketserver.TCPServer.__init__(self, server_address, RequestHandlerClass)
def __init__(self, server_address, RequestHandlerClass, bind_and_activate = True):
socketserver.BaseServer.__init__(self, server_address, RequestHandlerClass)
self.__shutdown_request = False
self.r_service, self.w_service = socket.socketpair()
@ -47,6 +47,25 @@ class KeyServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
else:
self._ServerSelector = selectors.SelectSelector
try:
ip_ver = ipaddress.ip_address(server_address[0])
except ValueError as e:
pretty_printer(log_obj = loggersrv.error, to_exit = True,
put_text = "{reverse}{red}{bold}%s. Exiting...{end}" %str(e))
if ip_ver.version == 4:
self.address_family = socket.AF_INET
elif ip_ver.version == 6:
self.address_family = socket.AF_INET6
self.socket = socket.socket(self.address_family, self.socket_type)
if bind_and_activate:
try:
self.server_bind()
self.server_activate()
except:
self.server_close()
raise
def pykms_serve(self):
""" Mixing of socketserver serve_forever() and handle_request() functions,
without elements blocking tkinter.
@ -157,7 +176,7 @@ loggersrv = logging.getLogger('logsrv')
# 'help' string - 'default' value - 'dest' string.
srv_options = {
'ip' : {'help' : 'The IPv6 address to listen on. The default is \"::\" (all interfaces).', 'def' : "::", 'des' : "ip"},
'ip' : {'help' : 'The IP address (IPv4 or IPv6) to listen on. The default is \"0.0.0.0\" (all interfaces).', 'def' : "0.0.0.0", 'des' : "ip"},
'port' : {'help' : 'The network port to listen on. The default is \"1688\".', 'def' : 1688, 'des' : "port"},
'epid' : {'help' : 'Use this option to manually specify an ePID to use. If no ePID is specified, a random ePID will be auto generated.',
'def' : None, 'des' : "epid"},