Here is a simple class I use to snoop the protocol while still being able to use Tellstick Live.
It's missing pretty much everything from common sense trough good design to errorhandling but it's only meant to do one thing; snoop the protocol. Hope this helps you on your way!
---8<---
Imports System.Text
Imports System.Net.Sockets
Imports System.Threading
Imports System.Net
Namespace TellstickMonoProxyServer
Class Server
' /server/access (file content) CNAME IP
' ----------------------------- -------------------------- ---------------
' 11:alice.telldus.comiAFCAs alice.machine.telldus.com 46.21.105.101
' 11:tanja.telldus.comiAFCAs tanja.machine.telldus.com 46.21.105.7
' 11:julia.telldus.comiAFCAs julia.machine.telldus.com 62.20.121.252
' 11:wilma.telldus.comiAFCAs wilma.machine.telldus.com 79.99.6.173
' 11:zaida.telldus.comiAFCA zaida.machine.telldus.com 46.21.105.98
Private m_LiveServerList() As String = {"alice.telldus.com", "tanja.telldus.com", "julia.telldus.com", "wilma.telldus.com", "zaida.telldus.com"}
Private m_LiveServerPort As Long = 45002
Private m_LiveServerTcpClient As TcpClient = New TcpClient
Private m_LiveServerStream As NetworkStream
Private m_LiveServerConnected As Boolean = False
' Tellstick Net server
Private m_TellstickServerPort As Long = 45002
Private m_TellstickConnected As Boolean = False
Private m_TellstickServerListener As TcpListener = New TcpListener(IPAddress.Any, m_TellstickServerPort)
Private m_TellstickServerStream As NetworkStream
Private m_TellstickServerRegisterReceived As String = vbNullString
' ***************************************************************************************************************
'
' ***************************************************************************************************************
Public Sub New()
m_TellstickServerListener.Start() ' Start local server
Do
' Get client connection
log("[INFO] Listening for Tellstick clients on port " & m_TellstickServerPort)
Dim _tcpClient As TcpClient = m_TellstickServerListener.AcceptTcpClient()
Dim _t = New Thread(New ParameterizedThreadStart(AddressOf _serverThread))
_t.Start(_tcpClient)
' Do nothing while client is connected
While _t.IsAlive
Thread.Sleep(100)
End While
Loop
End Sub
' ***************************************************************************************************************
' Serverthread
' ***************************************************************************************************************
Private Sub _serverThread(_tcpClient As TcpClient)
Dim RemoteIpEndpoint As IPEndPoint = _tcpClient.Client.RemoteEndPoint
Dim ClientIP As String = RemoteIpEndpoint.Address.ToString
Dim ClientPort As String = RemoteIpEndpoint.Port
Dim encoder As New ASCIIEncoding()
log("[SERVER] Client connected! IP:" & ClientIP & " Port:" & ClientPort)
log("[INFO] Client connected!")
m_TellstickConnected = True
m_TellstickServerStream = _tcpClient.GetStream()
Dim m As Byte() = New Byte(4095) {} ' 4k buffer should be more than enough
Dim b As Integer = 0
m_TellstickServerStream.ReadTimeout = 130 * 1000 ' Ping every 120 seconds
While _tcpClient.Connected
b = 0
Try
b = m_TellstickServerStream.Read(m, 0, 4096)
Catch
log("[INFO] No data or ping from Tellstick Net in 130 seconds. Disconnecting!")
Exit While
End Try
If Not b = 0 Then
Dim s As String = encoder.GetString(m, 0, b)
Select Case s.Split(":")(2)
Case ("8")
log("[STICK -> SERVER] Register? - " & s)
log("[SERVER -> STICK] Ok! - A=28::A:")
m_TellstickServerStream.Write(encoder.GetBytes("A=28::A:"), 0, encoder.GetByteCount("A=28::A:"))
' We have a valid string to register with. Let's connect to Telldus Live!
m_TellstickServerRegisterReceived = s
ConnectToLiveServer()
Case "4"
log("[STICK -> SERVER] Ping! - " & s)
log("[SERVER -> STICK] Pong! - 28:4df8af148e8cc310c85929fffaf4d3617c8492236:4:pong")
m_TellstickServerStream.Write(encoder.GetBytes("28:4df8af148e8cc310c85929fffaf4d3617c8492236:4:pong"), 0, encoder.GetByteCount("28:4df8af148e8cc310c85929fffaf4d3617c8492236:4:pong"))
' Proxy this
log("[SERVER -> LIVE] Ping! - " & s)
m_LiveServerStream.Write(m, 0, b)
Case "7"
log("[STICK -> LIVE] RF signal - " & s)
' Proxy this
m_LiveServerStream.Write(m, 0, b)
Case "3"
log("[STICK -> LIVE] ACK! - " & s)
' Proxy this
m_LiveServerStream.Write(m, 0, b)
Case Else
log("[STICK -> LIVE] UNKNOWN! - " & s)
' Proxy this
m_LiveServerStream.Write(m, 0, b)
End Select
End If
End While
log("[INFO] Client disconnected!")
m_TellstickConnected = False
m_TellstickServerStream = Nothing
m_TellstickServerRegisterReceived = vbNullString
_tcpClient.Close()
End Sub
' ***************************************************************************************************************
' Connect to live server
' ***************************************************************************************************************
Private Sub ConnectToLiveServer()
log("[SERVER] Connecting to live server.")
For Each servername In m_LiveServerList
log("[SERVER] Trying trying server: " & servername)
Try
m_LiveServerTcpClient.Connect(servername, m_LiveServerPort)
Catch
log("[SERVER] No luck...")
End Try
If m_LiveServerTcpClient.Connected = True Then
log("[SERVER] Connected!")
Exit For
End If
Next
If m_LiveServerTcpClient.Connected = True Then
Dim _t = New Thread(New ParameterizedThreadStart(AddressOf _proxyThread))
_t.Start(m_LiveServerTcpClient)
End If
End Sub
' ***************************************************************************************************************
' Proxythread
' ***************************************************************************************************************
Private Sub _proxyThread(_tcpClient As TcpClient)
Dim RemoteIpEndpoint As IPEndPoint = _tcpClient.Client.RemoteEndPoint
Dim ClientIP As String = RemoteIpEndpoint.Address.ToString
Dim ClientPort As String = RemoteIpEndpoint.Port
Dim encoder As New ASCIIEncoding()
log("[SERVER] Connected to Live server! IP:" & ClientIP & " Port:" & ClientPort)
m_LiveServerConnected = True
m_LiveServerStream = _tcpClient.GetStream()
Dim m As Byte() = New Byte(4095) {} ' 4k buffer should be more than enough
Dim b As Integer = 0
'm_LiveServerStream.ReadTimeout = 130 * 1000 ' Ping every 120 seconds
' Send register packet.
log("[SERVER -> LIVE] Register? - " & m_TellstickServerRegisterReceived)
m_LiveServerStream.Write(encoder.GetBytes(m_TellstickServerRegisterReceived), 0, encoder.GetByteCount(m_TellstickServerRegisterReceived))
While _tcpClient.Connected
b = 0
Try
b = m_LiveServerStream.Read(m, 0, 4096)
Catch
log("[INFO] No data or ping from Live server in 130 seconds. Disconnecting!")
Exit While
End Try
If Not b = 0 Then
Dim s As String = Encoder.GetString(m, 0, b)
Select Case s.Split(":")(2)
Case ("8")
log("[LIVE -> SERVER ] OK! - " & s)
Case "4"
log("[LIVE -> STICK] Command - " & s)
' Proxy this
m_TellstickServerStream.Write(m, 0, b)
Case Else
log("[LIVE -> STICK] UNKNOWN! - " & s)
' Proxy this
m_TellstickServerStream.Write(m, 0, b)
End Select
End If
End While
log("[INFO] Live server disconnected!")
m_LiveServerConnected = False
m_LiveServerStream = Nothing
_tcpClient.Close()
End Sub
' ***************************************************************************************************************
' Logger
' ***************************************************************************************************************
Private Sub log(s As String)
Console.WriteLine("{0:T} " & s, Now)
End Sub
End Class
End Namespace
--->8---