Compare commits

3 Commits

Author SHA1 Message Date
fde7d540ca [packages] WIP ppa/snap support 2023-11-11 03:16:14 +01:00
3e3725ec22 [actions/send_telegram] fix chatId type 2023-11-09 14:15:42 +01:00
9fa2c02c9d [triggers/receive_telegram] init 2023-11-09 14:15:31 +01:00
5 changed files with 218 additions and 13 deletions

View File

@@ -3,6 +3,7 @@ package actions
import (
"fmt"
"encoding/json"
//"strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@@ -11,7 +12,7 @@ import (
type SendTelegram struct {
Token string `json:"token"`
ChatId string `json:"chatId"`
ChatId int64 `json:"chatId"`
Message string `json:"message"`
TestMessage string `json:"testMessage"`
ActionChan ActionResultChan
@@ -27,7 +28,8 @@ func (s SendTelegram) sendMessage(message string) error {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
msg := tgbotapi.NewMessage(-746157642, message)
chatId := s.ChatId
msg := tgbotapi.NewMessage(chatId, message)
_, err = bot.Send(msg)
if err != nil {
@@ -60,7 +62,9 @@ func (s SendTelegram) Execute() {
}
func CreateSendTelegram(config internal.ActionConfig, c ActionResultChan) (SendTelegram, error) {
result := SendTelegram{}
result := SendTelegram{
ChatId: 0,
}
err := json.Unmarshal(config.Options, &result)
@@ -72,7 +76,7 @@ func CreateSendTelegram(config internal.ActionConfig, c ActionResultChan) (SendT
return SendTelegram{}, internal.OptionMissingError{"token"}
}
if result.ChatId == "" {
if result.ChatId == 0 {
return SendTelegram{}, internal.OptionMissingError{"chatId"}
}
@@ -106,7 +110,7 @@ func (p SendTelegram) GetExample() string {
"type": "SendTelegram",
"options": {
"token": "5349923487:FFGrETxa0pA29d02Akslw-lkwjdA92KAH2",
"chatId": "-832345892",
"chatId": -832345892,
"message": "attention, intruders got my device!",
"testMessage": "this is just a test, no worries"
}

18
etc/gokill/config.json Normal file
View File

@@ -0,0 +1,18 @@
[
{
"type": "Timeout",
"name": "example trigger",
"options": {
"duration": 5
},
"actions": [
{
"type": "Print",
"options": {
"message": "hello world"
}
}
]
}
]

View File

@@ -21,10 +21,12 @@
gotools
mdbook
olm
dpkg
];
};
packages = {
packages = rec {
gokill = pkgs.buildGoModule rec {
pname = "gokill";
version = "1.0";
@@ -36,6 +38,7 @@
];
postInstall = ''
cp -r ./etc $out/ #for .deb packages
'';
};
@@ -54,6 +57,16 @@
'';
};
gokillSnap = pkgs.snapTools.makeSnap {
meta = {
name = "gokill";
summary = "simple but efficient";
description = "this should be longer";
architectures = [ "amd64" ];
confinement = "classic";
apps.gokill.command = "${gokill}/bin/gokill";
};
};
docs = pkgs.callPackage (import ./docs/default.nix) { self = self; };
@@ -61,12 +74,52 @@
};
bundlers.gokillDeb = pkg: pkgs.stdenv.mkDerivation {
name = "deb-single-${pkg.name}";
buildInputs = [
pkgs.fpm
];
unpackPhase = "true";
buildPhase = ''
export HOME=$PWD
mkdir -p ./nix/store/
for item in "$(cat ${pkgs.referencesByPopularity pkg})"
do
cp -r $item ./nix/store/
done
mkdir -p ./bin
cp -r ${pkg}/bin/* ./bin/
mkdir -p ./etc
cp -r ${pkg}/etc/* ./etc/
chmod -R a+rwx ./nix
chmod -R a+rwx ./bin
chmod -R a+rwx ./etc
fpm -s dir -t deb --name ${pkg.name} nix bin etc
'';
installPhase = ''
mkdir -p $out
cp -r *.deb $out
'';
};
apps = {
docs = {
type = "app";
program = builtins.toString (pkgs.writeScript "docs" ''
${pkgs.python3}/bin/python3 -m http.server --directory ${self.packages."${system}".docs}/share/doc'');
};
exportDEB = {
type = "app";
program = builtins.toString (pkgs.writeScript "docs" ''
${pkgs.nix}/bin/nix bundle --bundler .#bundlers.${system}.gokillDeb .#packages.${system}.gokill'');
};
};
})) ({
@@ -90,12 +143,6 @@
duration = 10;
};
actions = [
{
type = "Shutdown";
options = {
};
stage = 2;
}
];
}
];
@@ -103,7 +150,6 @@
virtualisation.vmVariant.virtualisation.graphics = false;
}
];
};
in
nixos.config.system.build.vm;

View File

@@ -0,0 +1,136 @@
package triggers
import (
"fmt"
"encoding/json"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"unknown.com/gokill/internal"
"unknown.com/gokill/actions"
)
type ReceiveTelegram struct {
Token string `json:"token"`
ChatId int64 `json:"chatId"`
Message string `json:"message"`
action actions.Action
}
func (s ReceiveTelegram) Listen() {
bot, err := tgbotapi.NewBotAPI(s.Token)
if err != nil {
return //fmt.Errorf("ReceiveTelegram waitForMessage error: %s", err)
}
bot.Debug = false
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
msg := tgbotapi.NewMessage(-746157642, "BOT TEST MESSAGE")
bot.Send(msg)
chatId := s.ChatId
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil { // If we got a message
if(update.Message.Chat.ID != chatId) {
internal.LogDoc(s).Debugf("ReceiveTelegram received wrong ChatId. Got %s, wanted %s", update.Message.Chat.ID, s.ChatId)
continue
}
if(update.Message.Text != s.Message) {
internal.LogDoc(s).Debug("ReceiveTelegram received wrong Message")
continue
}
internal.LogDoc(s).Info("ReceiveTelegram received secret message")
actions.Fire(s.action)
}
}
}
func CreateReceiveTelegram(config internal.KillSwitchConfig) (ReceiveTelegram, error) {
result := ReceiveTelegram{
ChatId: 0,
}
err := json.Unmarshal(config.Options, &result)
if err != nil {
return ReceiveTelegram{}, fmt.Errorf("Error during CreateReceiveTelegram: %s", err)
}
if result.Token == "" {
return ReceiveTelegram{}, internal.OptionMissingError{"token"}
}
if result.ChatId == 0 {
return ReceiveTelegram{}, internal.OptionMissingError{"chadId"}
}
if result.Message == "" {
return ReceiveTelegram{}, internal.OptionMissingError{"message"}
}
action, err := actions.NewAction(config.Actions)
if err != nil {
return ReceiveTelegram{}, fmt.Errorf("Error during CreateReceiveTelegram: %s", err)
}
result.action = action
return result, nil
}
func (e ReceiveTelegram) Create(config internal.KillSwitchConfig) (Trigger, error) {
return CreateReceiveTelegram(config)
}
func (p ReceiveTelegram) GetName() string {
return "ReceiveTelegram"
}
func (p ReceiveTelegram) GetDescription() string {
return "Waits for a specific message in a given chat. Once the message is received, the trigger fires."
}
func (p ReceiveTelegram) GetExample() string {
return `
{
"type": "ReceiveTelegram",
"options": {
"token": "5349923487:FFGrETxa0pA29d02Akslw-lkwjdA92KAH2",
"chatId": -832345892,
"message": "secretmessagethatfiresthetrigger"
}
}
`
}
func (p ReceiveTelegram) GetOptions() []internal.ConfigOption {
return []internal.ConfigOption{
{
Name: "token",
Type: "string",
Description: "telegram bot token (ask botfather)",
Default: "",
},
{
Name: "chatId",
Type: "int",
Description: "chatId of group or chat you want the message be received from.",
Default: "",
},
{
Name: "message",
Type: "string",
Description: "actual message that, when received, fires the trigger",
Default: "",
},
}
}

View File

@@ -29,6 +29,7 @@ func NewTrigger(config internal.KillSwitchConfig) (Trigger, error) {
func GetAllTriggers() []DocumentedTrigger {
return []DocumentedTrigger{
EthernetDisconnect{},
ReceiveTelegram{},
TimeOut{},
UsbDisconnect{},
}