none
Created endpoint works for localhost only. RRS feed

  • Question

  • I created a simple service that takes posted data and raises an event when data is received.  When I test it with a script posting data on the localhost, and have the URI set to HTTP://127.0.0.1:8080/post_stats, the service receives the data and passes it on in the raised event.

    But I want an external service to be able to post_stats; let's say another system on my home's private network; in this case I want to use my system's current address of HTTP://192.168.0.x:8080/post_stats, (being a fairly standard private network URI), but the program does not receive the data.

    Now, in case you're wondering how much I explored and tried to solve this on my own, I wrote a fairly simply Python script that happily accepts the data coming in that URI; not only that, I used the Python script to echo the very same data to the localhost port for the Windows program, and it works just fine... so Python can receive the data on that URI just fine, it can echo it to the localhost address, and it all works... but having to run a Python "echo" script is not really an acceptable solution.

    Here's the basic code; I'm running it from within Visual Studio 2017, which is being run as administrator.

    Public Class StatService
        Private host As WebServiceHost = Nothing
        Private endpoint As ServiceEndpoint = Nothing
    
        Public Sub StartService(ByRef receiver As JSONReceiver, ByVal URI As String)
            If Not IsNothing(URI) Then
                receiver = New JSONReceiver()
                host = New WebServiceHost(receiver, New Uri(URI))
                endpoint = host.AddServiceEndpoint(GetType(IJSONReceiver), New ServiceModel.WebHttpBinding(), "")
                host.Open()
            End If
        End Sub
    
        Public Sub StopService()
            If Not IsNothing(host) Then
                Try
                    host.Close()
                Catch ex As Exception
    
                End Try
            End If
        End Sub
    End Class
    
    <ServiceContract()>
    Public Interface IJSONReceiver
        <OperationContract()>
        <WebInvoke(RequestFormat:=WebMessageFormat.Json)>
        Function post_stats(ByVal request As Stream) As String
    End Interface
    
    <ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single)>
    Public Class JSONReceiver
        Implements IJSONReceiver
    
        Public Event DataReceived(sender As Object, gamedata As JObject)
    
        Public Function post_stats(ByVal request As Stream) As String Implements IJSONReceiver.post_stats
            Dim logger As MyLog = MyLog.GetInstance()
            Dim reader As New StreamReader(request)
            Dim data As String = reader.ReadToEnd()
            If Not IsNothing(data) Then
                logger.Log(data, LogLevels.Debugging)
                Try
                    If data.Length < 1 Then
                        logger.Log("Invalid data length.", LogLevels.Errors)
                    Else
                        RaiseEvent DataReceived(Me, JObject.Parse(data))
                    End If
                Catch ex As Exception
                    logger.Log("In post_stats: " & ex.Message, LogLevels.Errors)
                End Try
            Else
                logger.Log("In post_stats: empty data.", LogLevels.Warnings)
            End If
            Return "OK"
        End Function
    End Class

    So the question is if this is a security issue, if I have to set something else to allow it to accept external connections?  What am I missing?

    TIA

    EDIT: I wanted to add that creating the service endpoint on the system's IP address ALSO works when being posted to by the Python echo script.

    IOW, the python script is receiving data on http://192.168.1.x:8080/post_stats, and echos it to http://192.168.1.x:8081/post_stats; the visual basic program is listening on the latter endpoint and receives the data with no problem.

    The visual basic program will just not receive it from a different host other than itself, even when listening on it's actual IP address and not it's localhost address.

    • Edited by FredJH Wednesday, August 23, 2017 7:25 PM Additional Info
    Wednesday, August 23, 2017 7:12 PM