svrooij / teams-monitor

Send your Teams Status to any webhook in realtime
https://www.nuget.org/packages/SvRooij.TeamsMonitor/
GNU General Public License v3.0
65 stars 4 forks source link

Possibility to use authenticated Proxy #9

Closed derjoerg closed 5 months ago

derjoerg commented 6 months ago

Hi,

would it be somehow possible to include the capability to use an authenticated proxy?

It is mentioned here https://www.scrapingbee.com/blog/csharp-httpclient-proxy/#using-an-authenticated-proxy-with-httpclient , but I don't have any C# skills.

svrooij commented 6 months ago

What do you want to accomplish?

Shouting "authenticated proxy" is not really a good issue description.

derjoerg commented 6 months ago

So,

I want to use your program with the webhook functionality behind an authenticated proxy.

So I kindly ask you if it would be possible to add to the httpclient you use for the webhook the possibility to define a proxy, username and password.

Regards

derjoerg commented 5 months ago

Hi,

sorry. I just realized that the my wording might be a bit misleading.

Here is my use case: I'm using Teams within a company-network. To reach the internet I need to use a proxy on which I have to authenticate myself with a username and password.

So when I just use your brilliant program and the webhook is fired, I receive an error "407 - Proxy Authentication Required".

I have no glue regarding C#, but looking at the reference (in my first post) it seems to be possible to add this.

I hope this makes my request more clear.

Joerg

derjoerg commented 5 months ago

To whom it may concern.

I helped myself with a small powershell script, which starts a local webserver, listen to the post-request of the "teams-monitor" and forwards it (through the proxy) to my destination (Home-Assistant).

# the Webhook destination
$dest = "https://<REPLACE-WITH-YOUR-DESTINATION>"

# Http Server
#$http = [System.Net.HttpListener]::new() 
$http = New-Object System.Net.HttpListener

# Hostname and port to listen on
$http.Prefixes.Add("http://localhost:8080/")

# Start the Http Server 
$http.Start()

# Log ready message to terminal 
if ($http.IsListening) {
    write-host " HTTP Server Ready!  " -f 'black' -b 'gre'
}

# Start "teams-monitor" and provide as webhook the just created local address
Start-Process "teams-monitor.exe" -ArgumentList "--webhook http://localhost:8080" -NoNewWindow

# Used to listen for requests
while ($http.IsListening) {
    # Get Request Url
    # When a request is made in a web browser the GetContext() method will return a request object
    $context = $http.GetContext()

    # http://localhost:8080/
    if ($context.Request.HttpMethod -eq 'POST' -and $context.Request.RawUrl -eq '/') {
        # decode the form post
        $FormContent = [System.IO.StreamReader]::new($context.Request.InputStream).ReadToEnd()

        # We can log the request to the terminal
        write-host "$($context.Request.UserHostAddress)  =>  $($context.Request.Url)" -f 'mag'
        Write-Host $FormContent -f 'Green'

        # the html/data
        [string]$html = "Success" 

        #respond to the request (not needed, but used it for development)
        $buffer = [System.Text.Encoding]::UTF8.GetBytes($html)
        $context.Response.ContentLength64 = $buffer.Length
        $context.Response.OutputStream.Write($buffer, 0, $buffer.Length)
        $context.Response.OutputStream.Close() 

    # Send the data to the webhook
    $proxy = ([System.Net.WebRequest]::GetSystemWebproxy()).GetProxy($dest)
    Invoke-RestMethod -Method 'Post' -Uri $dest -ContentType 'application/json' -Body $FormContent -Proxy $proxy -ProxyUseDefaultCredentials
    }

    # http://localhost:8080/quit'
    if ($context.Request.HttpMethod -eq 'GET' -and $context.Request.RawUrl -eq '/quit')
    {
        $http.Close()
    }

}