Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Http Listener
#1
I want to implement a port listening function similar to the PowerShell code below using the HttpListener function,
but I haven't succeeded.
After executing the function, I am unable to stop the listening service and return the Code value.  

Thank you in advance for any suggestions and help.

Function HttpListener_T
 
Code:
Copy      Help
out 

;https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade
;After registering the application from the above link, obtain the following parameters:
str clientID     = "a6eb8463-d6d9-4466-b774-29b5f2bb66"
str clientSecret = "r.I8Q~O5UKQU~9RZNn7~uCinjnDR~Z2hagY5Ob"
str redirectUri  = "http://localhost:7654/"

str uri =
F
;https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={clientID}
;scope=offline_access files.readwrite
;response_type=code
;redirect_uri={redirectUri}
uri.findreplace("[]" "&")

run uri

out HttpListener(7654)

Function HttpListener
 
Code:
Copy      Help
function~ port

if(getopt(nthreads)>1) ret

#compile "__TcpSocket"

AddTrayIcon "close.ico" "TcpSocket_server[]Ctrl+click to end."
str code
TcpSocket x.ServerStart(port &sub.OnClientConnected &code)

x.Close
ret code

#sub OnClientConnected
function TcpSocket&client $clientIp str&code !*reserved

str method path headers
if(!client.HttpRead(method path headers)) ret

out "method:[]%s"method
out "path:[]%s"path

if method="GET" and !empty(path)
,_s.get(path find(path "=")+1)
,code=_s

Powershell Code:
Code:
Copy      Help
# -- https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade
# -- After registering the application from the above link, obtain the following parameters:
$clientID     = 'a6eb8463-d6d9-4466-b774-29b5f2bb66'
$clientSecret = 'r.I8Q~O5UKQU~9RZNn7~uCinjnDR~Z2hagY5Ob'
$redirectUri  = 'http://localhost:7654/'

# -- Microsoft Graph OAuth2.0 Authorization Code Flow
$uri = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={0}&scope=offline_access files.readwrite&response_type=code&redirect_uri={1}' -f $clientID, $redirectUri

start $uri

#################################################### Implement the following functionality in the HttpListener function of qm.
$listener = New-Object 'System.Net.HttpListener'
$listener.Prefixes.Add($redirectUri)
$listener.Start()
$ctx  = $listener.GetContext()
$code = $ctx.Request.QueryString.GetValues('code') | select -First 1
if($code -eq $null){
    $bytes = [Text.Encoding]::Default.GetBytes('Authorization Failed')
} else {
    $bytes = [Text.Encoding]::Default.GetBytes('Authorization Successful, Please Close the Current Page')
}
$ctx.Response.ContentLength64 = $bytes.Length
$ctx.Response.OutputStream.Write($bytes, 0, $bytes.Length)
$listener.Stop()
$ctx.Response.Close()
$listener.Dispose()
#2
I’ve tried many methods, but I can’t close the TCP service at the appropriate time.


Forum Jump:


Users browsing this thread: 1 Guest(s)