sshpoke/pkg/convert/convert.go

80 lines
1.9 KiB
Go

package convert
import (
"net"
"github.com/Neur0toxine/sshpoke/pkg/dto"
"github.com/Neur0toxine/sshpoke/pkg/plugin/pb"
)
func MessageToAppEvent(event *pb.EventMessage) dto.Event {
return dto.Event{
Type: MessageEventTypeToApp(event.GetType()),
Container: dto.Container{
ID: event.GetContainer().GetId(),
Names: event.GetContainer().GetNames(),
IP: net.ParseIP(event.GetContainer().GetIp()),
Port: uint16(event.GetContainer().GetPort()),
Server: event.GetContainer().GetServer(),
Prefix: event.GetContainer().GetPrefix(),
Domain: event.GetContainer().GetDomain(),
},
}
}
func AppEventToMessage(event dto.Event) *pb.EventMessage {
return &pb.EventMessage{
Type: AppEventTypeToMessage(event.Type),
Container: &pb.Container{
Id: event.Container.ID,
Names: event.Container.Names,
Ip: event.Container.IP.String(),
Port: uint32(event.Container.Port),
Server: event.Container.Server,
Prefix: event.Container.Prefix,
Domain: event.Container.Domain,
},
}
}
func MessageToAppEventStatus(val *pb.EventStatusMessage) dto.EventStatus {
return dto.EventStatus{
Type: MessageEventTypeToApp(val.GetType()),
ID: val.GetId(),
Error: val.GetError(),
Domain: val.GetDomain(),
}
}
func AppEventStatusToMessage(status dto.EventStatus) *pb.EventStatusMessage {
return &pb.EventStatusMessage{
Type: AppEventTypeToMessage(status.Type),
Id: status.ID,
Error: status.Error,
Domain: status.Domain,
}
}
func MessageEventTypeToApp(typ pb.EventType) dto.EventType {
val := dto.EventType(typ.Number())
if val < dto.EventStart || val > dto.EventUnknown {
return dto.EventUnknown
}
return val
}
func AppEventTypeToMessage(typ dto.EventType) pb.EventType {
switch typ {
case 0:
return pb.EventType_EVENT_START
case 1:
return pb.EventType_EVENT_STOP
case 2:
return pb.EventType_EVENT_SHUTDOWN
case 3:
return pb.EventType_EVENT_ERROR
default:
return pb.EventType_EVENT_UNKNOWN
}
}