SystemRage / py-kms

KMS Server Emulator written in Python
The Unlicense
2.08k stars 643 forks source link

windows service tutorial is not working now #108

Closed muhammetunal closed 4 years ago

muhammetunal commented 4 years ago

in windows 2016 server when I run this code

PS C:\python> C:\Python\python.exe C:\Python\py-kms\pykms_Server.py SO_REUSEPORT not supported on this platform. Exiting...

I got above error,

but when I run it with parameters as below (I learned it from the png file that systemrage sent for implying color issues)

PS C:\Python> C:\Python\python.exe C:\Python\py-kms\pykms_Server.py connect -u 

                                                            Client sending

Server received RPC Bind Request !!! <===============

Server parsing RPC Bind Request...

Server generating RPC Bind Response...

It is working

Now here my question: I use the code below from tutorial to create a service but it is notworking as of now. I tried to add the "connect -u" parameter, but couldnt success in that either, is it really necassary the "connect -u" parameter in order so start the server? or is there any other way that I am missing. note that Old versions were working without any parameters so the code below was working.

import win32serviceutil import win32service import win32event import servicemanager import socket import subprocess

class AppServerSvc (win32serviceutil.ServiceFramework): _svcname = "py-kms" _svc_displayname = "py-kms" _proc = None _cmd = ["C:\Python\python.exe", "C:\Python\py-kms\pykms_Server.py"]

def __init__(self,args):
    win32serviceutil.ServiceFramework.__init__(self,args)
    self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    socket.setdefaulttimeout(60)

def SvcStop(self):
    self.killproc()
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_,''))
    self.main()

def main(self):
    self._proc = subprocess.Popen(self._cmd)
    self._proc.wait()

def killproc(self):
    self._proc.kill()

if name == 'main': win32serviceutil.HandleCommandLine(AppServerSvc)

simonmicro commented 4 years ago

note that Old versions were

Could you be more specific on that?

I tried to add the "connect -u" parameter, but couldnt success in that either

Yesterday I added an overwrite, which enables this parameter always (disables the socket port reuse), when running inside an Windows Sandox Vm - I doubt it, but maybe thats broken?

        all_address = [(
                        srv_config['ip'], srv_config['port'],
                        (srv_config['backlog_main'] if 'backlog_main' in srv_config else srv_options['backlog']['def']),
                        (srv_config['reuse_main'] if 'reuse_main' in srv_config else False if getpass.getuser() == 'WDAGUtilityAccount' else srv_options['reuse']['def'])
                        )]
        log_address = "TCP server listening at %s on port %d" %(srv_config['ip'], srv_config['port'])

Normally this all should work fine - can anyone else reproduce this? As my testing systems had no problems with it (Linux, Windows Sandbox).

And maybe you could cleanup your issue a little bit?

muhammetunal commented 4 years ago

Hi thanks for kind reply, firstly my windows is not virtualised. it is bare windows server standard 2019 v1809 running on an old mac book pro mid 2012 I tried to add that "connect -u" parameter to the kms-winservice.py file in order to make it work. this file is decribed in windows services tutorial. I also added it in my first mail. no doubt I use the latest version, I double checked it. and see the code you try to fix. but it is not working for me. I get this error

PS C:\python\py-kms> .\pykms_Server.py SO_REUSEPORT not supported on this platform. Exiting...

we can fix this either with editing pykms_server.py or with kms-winservice.py. I prefer doing it in pykms_server.py. this will be better for code reusability.

so I am open to any sugessions. thanks in advance

simonmicro commented 4 years ago

Okay. When you System does not support port reusing you'll need to add connect -u to pykms_Server.py. Just a quick check: Please insert the following code in place of the current fix (somewhere in pykms_Server.py) for such cases - does it work then?

        all_address = [(
                        srv_config['ip'], srv_config['port'],
                        (srv_config['backlog_main'] if 'backlog_main' in srv_config else srv_options['backlog']['def']),
                        (False)
                        )]
muhammetunal commented 4 years ago

sorry for the late reply, yes with this fix now it is working.

muhammetunal commented 4 years ago

but I belive I have to reapply this fix everytime a new version of this pykms_Server.py file will be committed.

simonmicro commented 4 years ago

Yes you have, because it just disabled the port reusing condition-less. But I have no idea, why the current version is not working, because... Well, it should!

SystemRage commented 4 years ago

@muhammetunal you said that old versions were working, but you refer versions before 328dced ? (if yes you're right they work for you, SO_REUSEPORT is disabled) After that i kept SO_REUSEPORT always enabled (and only now you can choose !!).

class KeyServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
        daemon_threads = True
        allow_reuse_address = True
muhammetunal commented 4 years ago

@SystemRage Yes you are right working class is keyserver, and not keyserverhandler. and It is working with the @Simonmicro 's fix so I can close the case. thank you for your helps :)

would it be possible that cause might be my server also has an windows linux subsystem (but I am running this application on native windows python subsystem.) because it enables virtualization somehow.