feat: add basic framework for udp reporter
This commit is contained in:
parent
5fed993f73
commit
9b8bb34378
@ -1,25 +1,34 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"net/url"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
socketio "github.com/googollee/go-socket.io"
|
"net/url"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
URL = flag.String("url", "", "The http url of tianji, for example: https://tianji.msgbyte.com")
|
Url = flag.String("url", "", "The http url of tianji, for example: https://tianji.msgbyte.com")
|
||||||
WORKSPACE_ID = flag.String("workspace", "", "The workspace id for tianji, this should be a uuid")
|
WorkspaceId = flag.String("workspace", "", "The workspace id for tianji, this should be a uuid")
|
||||||
INTERVAL = flag.Float64("interval", 2.0, "Input the INTERVAL")
|
Name = flag.String("name", "", "The identification name for this machine")
|
||||||
isVnstat = flag.Bool("vnstat", false, "Use vnstat for traffic statistics, linux only")
|
Interval = flag.Float64("interval", 5.0, "Input the INTERVAL")
|
||||||
|
IsVnstat = flag.Bool("vnstat", false, "Use vnstat for traffic statistics, linux only")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ReportData struct {
|
||||||
|
WorkspaceId string `json:"workspaceId"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Hostname string `json:"hostname"`
|
||||||
|
Payload any `json:"payload"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
parsedURL, err := url.Parse(*URL)
|
parsedURL, err := url.Parse(*Url)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Invalid URL:", err)
|
log.Fatal("Invalid URL:", err)
|
||||||
@ -29,14 +38,55 @@ func main() {
|
|||||||
log.Fatal("Invalid URL: Missing scheme")
|
log.Fatal("Invalid URL: Missing scheme")
|
||||||
}
|
}
|
||||||
|
|
||||||
if *WORKSPACE_ID == "" {
|
if *WorkspaceId == "" {
|
||||||
log.Fatal("WORKSPACE_ID must not be blank!")
|
log.Fatal("WORKSPACE_ID must not be blank!")
|
||||||
}
|
}
|
||||||
|
|
||||||
client, _ := socketio.NewClient(*URL, nil)
|
hostname, _ := os.Hostname()
|
||||||
|
var name string
|
||||||
client.Connect()
|
if *Name != "" {
|
||||||
client.Emit("notice", "hello")
|
name = *Name
|
||||||
|
} else {
|
||||||
fmt.Println("Hello, World!", *URL)
|
name = hostname
|
||||||
|
}
|
||||||
|
|
||||||
|
sendUDPTestPack(*parsedURL, ReportData{
|
||||||
|
WorkspaceId: *WorkspaceId,
|
||||||
|
Name: name,
|
||||||
|
Hostname: hostname,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendUDPTestPack(url url.URL, payload ReportData) {
|
||||||
|
// parse target url
|
||||||
|
addr, err := net.ResolveUDPAddr("udp", url.Hostname()+":"+url.Port())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error resolving address:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create UDP connection
|
||||||
|
conn, err := net.DialUDP("udp", nil, addr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error creating connection:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
// serialized message
|
||||||
|
jsonData, err := json.Marshal(payload)
|
||||||
|
fmt.Printf("[Report] %s\n", jsonData)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error encoding JSON:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send message
|
||||||
|
_, err = conn.Write(jsonData)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error sending message:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Message sent successfully!")
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,14 @@ import { workspaceRouter } from './router/workspace';
|
|||||||
import { telemetryRouter } from './router/telemetry';
|
import { telemetryRouter } from './router/telemetry';
|
||||||
import { initSocketio } from './ws';
|
import { initSocketio } from './ws';
|
||||||
import { trpcExpressMiddleware } from './trpc';
|
import { trpcExpressMiddleware } from './trpc';
|
||||||
|
import { initUdpServer } from './udp/server';
|
||||||
|
|
||||||
const port = Number(process.env.PORT || 12345);
|
const port = Number(process.env.PORT || 12345);
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
initUdpServer(port);
|
||||||
|
|
||||||
initSocketio(app);
|
initSocketio(app);
|
||||||
|
|
||||||
app.use(compression());
|
app.use(compression());
|
||||||
|
34
src/server/udp/server.ts
Normal file
34
src/server/udp/server.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import dgram from 'dgram';
|
||||||
|
|
||||||
|
export function initUdpServer(port: number) {
|
||||||
|
const server = dgram.createSocket('udp4');
|
||||||
|
|
||||||
|
server.on('error', (err) => {
|
||||||
|
console.log(`Init error:\n${err.stack}`);
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on('message', (msg, rinfo) => {
|
||||||
|
try {
|
||||||
|
const raw = String(msg);
|
||||||
|
const json = JSON.parse(String(msg));
|
||||||
|
const { workspaceId, name, hostname, payload } = json;
|
||||||
|
|
||||||
|
if (!workspaceId || !name || !hostname) {
|
||||||
|
console.warn(
|
||||||
|
'[UDP] lost some necessary params, request will be ignore',
|
||||||
|
json
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('recevice tianji report:', raw, 'info', rinfo);
|
||||||
|
} catch (err) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on('listening', () => {
|
||||||
|
const address = server.address();
|
||||||
|
console.log(`UDP Server is listening: ${address.address}:${address.port}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.bind(port);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user