-
Notifications
You must be signed in to change notification settings - Fork 5
/
commandline.go
1055 lines (934 loc) · 28.9 KB
/
commandline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/DENICeG/go-rriclient/pkg/rri"
"github.com/sbreitf1/go-console"
"github.com/sbreitf1/go-console/commandline"
"github.com/sbreitf1/go-console/input"
)
var (
//TODO configure colors and signs from external file
colorPromptRRI = "\033[1;34m"
colorPromptUser = "\033[1;32m"
colorPromptHost = "\033[1;32m"
colorSendRaw = "\033[0;94m"
colorReceiveRaw = "\033[0;96m"
colorSuccessResponse = "\033[0;29m"
colorErrorResponseMessage = "\033[0;91m"
colorTechnicalErrorMessage = "\033[1;91m"
colorInnerError = "\033[2;91m"
colorEnd = "\033[0m"
signSend = "-->"
signReceive = "<--"
cleRRIClient *rri.Client
histDomains *domainHistory
histHandles *handleHistory
returnErrorOnFail = false
customCommands []customCommand
)
func disableColors() {
colorPromptRRI = ""
colorPromptUser = ""
colorPromptHost = ""
colorSendRaw = ""
colorReceiveRaw = ""
colorSuccessResponse = ""
colorErrorResponseMessage = ""
colorTechnicalErrorMessage = ""
colorInnerError = ""
colorEnd = ""
}
func printColorsAndSigns() {
printColor := func(name, colorStr string) {
console.Printlnf(" %s%s%s", colorStr, name, colorEnd)
}
printSign := func(name, sign string) {
console.Printlnf(" %s: %q", name, sign)
}
printColor("ColorDefault", colorEnd)
printColor("ColorPromptRRI", colorPromptRRI)
printColor("ColorPromptUser", colorPromptUser)
printColor("ColorPromptHost", colorPromptHost)
printColor("ColorSendRaw", colorSendRaw)
printColor("ColorReceiveRaw", colorReceiveRaw)
printColor("ColorSuccessResponse", colorSuccessResponse)
printColor("ColorErrorResponseMessage", colorErrorResponseMessage)
printColor("ColorTechnicalErrorMessage", colorTechnicalErrorMessage)
printColor("ColorInnerError", colorInnerError)
printSign("SignSend", signSend)
printSign("SignReceive", signReceive)
}
func runCLE(confDir string, client *rri.Client, cmd []string) error {
cleRRIClient = client
histDomains = &domainHistory{make([]string, 0)}
histHandles = &handleHistory{make([]string, 0)}
if !console.SupportsColors() {
disableColors()
}
var err error
customCommands, err = readCustomCommands(filepath.Join(confDir, "custom-commands"))
if err != nil {
if !os.IsNotExist(err) {
console.Println("Failed to import custom commands:", err.Error())
}
}
cle := prepareCLE()
if len(cmd) > 0 {
// exec command that has been passed via command line and return result
return cle.ExecCommand(cmd[0], cmd[1:])
}
console.Println("Interactive RRI Command Line")
console.Println(" type 'help' to see a list of available commands")
console.Println(" use tab for auto-completion and arrow keys for history")
// start interactive command line loop
if err := cle.Run(); err != nil {
if commandline.IsErrCtrlC(err) {
console.Println()
return nil
}
return err
}
return nil
}
type customCommand struct {
DisabledFor []string `json:"disable-for-env"`
Name string `json:"name"`
Cmd string `json:"cmd"`
Description string `json:"description"`
Action string `json:"action"`
Args []customCommandArg `json:"args"`
}
type customCommandArg struct {
Name string `json:"name"`
Type string `json:"type"`
Field string `json:"field"`
Value string `json:"value"`
}
func (arg customCommandArg) IsInputParameter() bool {
return strings.ToLower(arg.Type) == "domain"
}
func readCustomCommands(dir string) ([]customCommand, error) {
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
customCommands := make([]customCommand, 0)
for _, f := range files {
if !f.IsDir() {
data, err := os.ReadFile(filepath.Join(dir, f.Name()))
if err != nil {
return nil, err
}
var cmd customCommand
if err := json.Unmarshal(data, &cmd); err != nil {
return nil, fmt.Errorf("could not import %q: %s", f.Name(), err.Error())
}
if len(cmd.Name) == 0 {
cmd.Name = f.Name()
}
if len(cmd.Cmd) == 0 {
cmd.Cmd = f.Name()
}
for i := range cmd.Args {
if strings.ToLower(cmd.Args[i].Type) == "domain" && len(cmd.Args[i].Field) == 0 {
cmd.Args[i].Field = "domain"
cmd.Args[i].Name = "domain"
}
}
customCommands = append(customCommands, cmd)
}
}
return customCommands, nil
}
func prepareCLE() *commandline.Environment {
cle := commandline.NewEnvironment()
cle.Prompt = func() string {
var prefix, user, host, suffix string
prefix = fmt.Sprintf("%sRRI%s", colorPromptRRI, colorEnd)
if cleRRIClient.IsLoggedIn() {
user = fmt.Sprintf("%s%s%s@", colorPromptUser, cleRRIClient.CurrentUser(), colorEnd)
}
host = fmt.Sprintf("%s%s%s", colorPromptHost, cleRRIClient.RemoteAddress(), colorEnd)
return fmt.Sprintf("%s{%s%s}%s", prefix, user, host, suffix)
}
cle.ErrorHandler = func(cmd string, args []string, err error) error {
console.Printlnf("%sERROR: %s%s", colorTechnicalErrorMessage, err.Error(), colorEnd)
return nil
}
cle.RegisterCommand(commandline.NewExitCommand("exit"))
cle.RegisterCommand(commandline.NewCustomCommand("help", nil, cmdHelp))
cle.RegisterCommand(commandline.NewCustomCommand("login", nil, cmdLogin))
cle.RegisterCommand(commandline.NewCustomCommand("logout", nil, cmdLogout))
registerSwitchCommand(cle, "create", cmdSwitches{
Domain: cmdCreateDomain,
Handle: cmdCreateHandle,
AuthInfo1: cmdCreateAuthInfo1,
})
registerSwitchCommand(cle, "check", cmdSwitches{
Domain: newDomainQueryCommand(rri.NewCheckDomainQuery),
Handle: newHandleQueryCommand(rri.NewCheckHandleQuery),
})
registerSwitchCommand(cle, "info", cmdSwitches{
Domain: newDomainQueryCommand(rri.NewInfoDomainQuery),
Handle: newHandleQueryCommand(rri.NewInfoHandleQuery),
})
registerSwitchCommand(cle, "update", cmdSwitches{
Domain: cmdUpdateDomain,
})
registerDomainCommand(cle, "delete", newDomainQueryCommand(rri.NewDeleteDomainQuery))
registerDomainCommand(cle, "restore", newDomainQueryCommand(rri.NewRestoreDomainQuery))
registerDomainCommand(cle, "transit", cmdTransit, commandline.NewOneOfArgCompletion("disconnect", "connect"))
registerDomainCommand(cle, "chholder", cmdChangeHolder)
registerDomainCommand(cle, "chprov", cmdChangeProvider)
registerDomainCommand(cle, "queue-read", cmdQueueRead)
registerDomainCommand(cle, "queue-delete", cmdQueueDelete)
// register custom commands
for _, cmd := range customCommands {
registerCustomCommand(cle, cmd)
}
cle.RegisterCommand(commandline.NewCustomCommand("raw", nil, cmdRaw))
cle.RegisterCommand(commandline.NewCustomCommand("file", commandline.NewFixedArgCompletion(commandline.NewLocalFileSystemArgCompletion(true)), cmdFile))
cle.RegisterCommand(commandline.NewCustomCommand("xml", nil, cmdXML))
cle.RegisterCommand(commandline.NewCustomCommand("verbose", nil, cmdVerbose))
return cle
}
func cmdHelp(args []string) error {
type customCmd struct {
Cmd, Args []string
Desc string
}
commands := []customCmd{
{[]string{"exit"}, nil, "exit application"},
{[]string{"help"}, nil, "show this help"},
{},
{[]string{"login"}, []string{"user", "password"}, "log in to a RRI account"},
{[]string{"logout"}, nil, "log out from the current RRI account"},
{},
{[]string{"create", "handle"}, []string{"domain"}, "send a CREATE command for a specific handle"},
{[]string{"check", "handle"}, []string{"domain"}, "send a CHECK command for a specific handle"},
{[]string{"info", "handle"}, []string{"domain"}, "send an INFO command for a specific handle"},
//TODO contact-update
{},
{[]string{"create", "domain"}, []string{"domain"}, "send a CREATE command for a new domain"},
{[]string{"check", "domain"}, []string{"domain"}, "send a CHECK command for a specific domain"},
{[]string{"info", "domain"}, []string{"domain"}, "send an INFO command for a specific domain"},
{[]string{"update", "domain"}, []string{"domain"}, "send an UPDATE command for a specific domain"},
{[]string{"chholder", "domain"}, []string{"domain"}, "send an CHHOLDER command for a specific domain"},
{},
{[]string{"delete"}, []string{"domain"}, "send a DELETE command for a specific domain"},
{[]string{"restore"}, []string{"domain"}, "send a RESTORE command for a specific domain"},
{[]string{"transit"}, []string{"domain"}, "send a TRANSIT command for a specific domain"},
{[]string{"create", "authinfo1"}, []string{"domain", "secret", "expire"}, "send a CREATE-AUTHINFO1 command for a specific domain"},
//TODO create-authinfo2
//TODO delete-authinfo1
{[]string{"chprov"}, []string{"domain", "secret"}, "send a CHPROV command for a specific domain"},
{},
{[]string{"verify-queue-read"}, nil, "send a VERIFY-QUEUE-READ command"},
{[]string{"verify-queue-delete"}, []string{"msgid"}, "send a VERIFY-QUEUE-DELETE command for a specific vChecked message"},
//TODO verify
// -
//TODO queue-read
//TODO queue-delete
//TODO regacc-info
{},
{[]string{"raw"}, nil, "enter a raw query and send it"},
{[]string{"file"}, []string{"path"}, "process a query file as accepted by flag --file"},
{},
{[]string{"xml"}, nil, "toggle XML mode"},
{[]string{"verbose"}, nil, "toggle verbose mode"},
}
if len(customCommands) > 0 {
head := commands[:25]
tail := make([]customCmd, 6)
copy(tail, commands[25:])
commands = head
for _, cmd := range customCommands {
args := make([]string, 0)
for _, arg := range cmd.Args {
if arg.IsInputParameter() {
args = append(args, arg.Name)
}
}
commands = append(commands, customCmd{[]string{cmd.Cmd}, args, cmd.Description})
}
commands = append(commands, customCmd{})
commands = append(commands, tail...)
}
cmdSumStrings := make([]string, len(commands))
argsSumStrings := make([]string, len(commands))
console.Println("Available commands:")
// measure command info texts to find the best fitting display variant for the current tty
maxCmdSumLen := 0
maxCmdLen := 0
maxArgsSumLen := 0
maxArgsLen := 0
maxDescLen := 0
for i, c := range commands {
for _, a := range c.Cmd {
if len(cmdSumStrings[i]) == 0 {
cmdSumStrings[i] += a
} else {
cmdSumStrings[i] += " " + a
}
if len([]rune(a)) > maxCmdLen {
maxCmdLen = len([]rune(a))
}
}
if len([]rune(cmdSumStrings[i])) > maxCmdSumLen {
maxCmdSumLen = len([]rune(cmdSumStrings[i]))
}
for _, a := range c.Args {
if len(argsSumStrings[i]) == 0 {
argsSumStrings[i] += "{" + a + "}"
} else {
argsSumStrings[i] += " {" + a + "}"
}
if len([]rune(a)) > maxArgsLen {
maxArgsLen = len([]rune(a))
}
}
if len([]rune(argsSumStrings[i])) > maxArgsSumLen {
maxArgsSumLen = len([]rune(argsSumStrings[i]))
}
if len([]rune(c.Desc)) > maxDescLen {
maxDescLen = len([]rune(c.Desc))
}
}
// sum of spacers and placeholders in " NAME ARGS_SUM - DESC"
maxLineLength := 2 + maxCmdSumLen + 1 + maxArgsSumLen + 2 + 1 + 2 + maxDescLen
w, _, err := console.GetSize()
if w <= 0 || maxLineLength <= w || err != nil {
// nice table for wide terminals
for i, c := range commands {
if c.Cmd == nil {
console.Println()
} else {
leftLen := len([]rune(cmdSumStrings[i])) + 1 + len([]rune(argsSumStrings[i]))
console.Printlnf(" %s %s%s - %s", cmdSumStrings[i], argsSumStrings[i], strings.Repeat(" ", maxCmdSumLen+1+maxArgsSumLen-leftLen), c.Desc)
}
}
} else {
if maxCmdSumLen+1+maxArgsSumLen <= w {
// print narrow version with two separate lines for command+args and description
for i, c := range commands {
if c.Cmd == nil {
console.Println()
} else {
if len(c.Args) > 0 {
console.Printlnf("%s %s", cmdSumStrings[i], argsSumStrings[i])
} else {
console.Printlnf("%s", cmdSumStrings[i])
}
if len([]rune(c.Desc)) <= w-2 {
console.Printlnf(" %s", c.Desc)
} else {
// break words of description to multiple lines with indentation
parts := strings.Split(c.Desc, " ")
lines := make([]string, 1)
for _, p := range parts {
l := lines[len(lines)-1]
if len(l) > 0 {
l += " " + p
} else {
l += p
}
if len([]rune(l)) < w-2 || len(lines[len(lines)-1]) == 0 {
lines[len(lines)-1] = l
} else {
lines = append(lines, p)
}
}
for _, l := range lines {
console.Printlnf(" %s", l)
}
}
}
}
} else {
// fallback variant for very narrow terminals
for i, c := range commands {
if c.Cmd == nil {
console.Println()
} else {
if len(c.Args) > 0 {
console.Printlnf("-> %s %s - %s", cmdSumStrings[i], argsSumStrings[i], c.Desc)
} else {
console.Printlnf("-> %s - %s", cmdSumStrings[i], c.Desc)
}
}
}
}
}
return nil
}
func addToListRemoveDouble(list []string, new ...string) []string {
newList := make([]string, 0)
for _, str := range list {
// filter out all values equal to one in new (ignore case)
found := false
for _, newStr := range new {
if strings.EqualFold(str, newStr) {
found = true
break
}
}
if !found {
newList = append(newList, str)
}
}
return append(new, newList...)
}
type domainHistory struct {
list []string
}
func (h *domainHistory) Put(domain string) {
h.list = addToListRemoveDouble(h.list, domain)
}
func (h *domainHistory) GetCompletionOptions(currentCommand []string, entryIndex int) []commandline.CompletionOption {
return commandline.PrepareCompletionOptions(h.list, false)
}
type handleHistory struct {
list []string
}
func (h *handleHistory) Put(handle string) {
h.list = addToListRemoveDouble(h.list, handle)
}
func (h *handleHistory) GetCompletionOptions(currentCommand []string, entryIndex int) []commandline.CompletionOption {
options := commandline.PrepareCompletionOptions(h.list, false)
regAccID, err := cleRRIClient.CurrentRegAccID()
if err == nil {
return append(options, commandline.PrepareCompletionOptions([]string{fmt.Sprintf("DENIC-%d-", regAccID)}, true)...)
}
return options
}
func (h *handleHistory) GetHistoryEntry(index int) (string, bool) {
if index >= len(h.list) {
return "", false
}
return h.list[index], true
}
type domainOrHandleCompletion struct{}
func (domainOrHandleCompletion) GetCompletionOptions(currentCommand []string, entryIndex int) []commandline.CompletionOption {
if len(currentCommand) >= 2 && entryIndex == 2 {
if currentCommand[1] == "domain" || currentCommand[1] == "authinfo1" {
return histDomains.GetCompletionOptions(currentCommand, entryIndex)
}
if currentCommand[1] == "handle" {
return histHandles.GetCompletionOptions(currentCommand, entryIndex)
}
}
return nil
}
type cmdSwitches struct {
Domain commandline.ExecCommandHandler
Handle commandline.ExecCommandHandler
AuthInfo1 commandline.ExecCommandHandler
}
func registerSwitchCommand(cle *commandline.Environment, name string, switches cmdSwitches) {
// assemble arg completion
types := make([]string, 0)
if switches.Domain != nil {
types = append(types, "domain")
}
if switches.Handle != nil {
types = append(types, "handle")
}
if switches.AuthInfo1 != nil {
types = append(types, "authinfo1")
}
cle.RegisterCommand(commandline.NewCustomCommand(name,
commandline.NewFixedArgCompletion(
commandline.NewOneOfArgCompletion(types...),
domainOrHandleCompletion{},
),
func(args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing command type")
}
// explicit type is given
if args[0] == "domain" && switches.Domain != nil {
// remove type parameter
return switches.Domain(args[1:])
}
if args[0] == "handle" && switches.Handle != nil {
// remove type parameter
return switches.Handle(args[1:])
}
if args[0] == "authinfo1" && switches.AuthInfo1 != nil {
// remove type parameter
return switches.AuthInfo1(args[1:])
}
// try to guess type from first parameter
if isDomainName(args[0]) {
if switches.Domain != nil {
return switches.Domain(args)
}
if switches.AuthInfo1 != nil {
return switches.AuthInfo1(args)
}
}
if isHandle(args[0]) && switches.Handle != nil {
return switches.Handle(args)
}
// assemble expect string
str := ""
for i, t := range types {
switch {
case i == 0:
str += "'" + t + "'"
case i == len(types)-1:
str += " or '" + t + "'"
default:
str += ", '" + t + "'"
}
}
return fmt.Errorf("unknown command type. expect %s", str)
}))
}
func isDomainName(str string) bool {
return strings.HasSuffix(str, ".de")
}
func isHandle(str string) bool {
return strings.HasPrefix(str, "DENIC-")
}
func registerDomainCommand(cle *commandline.Environment, name string, commandHandler commandline.ExecCommandHandler, additionalCompletionHandlers ...commandline.ArgCompletion) {
argCompletionHandlers := []commandline.ArgCompletion{histDomains}
argCompletionHandlers = append(argCompletionHandlers, additionalCompletionHandlers...)
cle.RegisterCommand(commandline.NewCustomCommand(name, commandline.NewFixedArgCompletion(argCompletionHandlers...), commandHandler))
}
func newDomainQueryCommand(f func(domain string) *rri.Query) commandline.ExecCommandHandler {
return func(args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing domain name")
}
_, err := processQuery(f(args[0]))
if histDomains != nil {
histDomains.Put(args[0])
}
return err
}
}
func newHandleQueryCommand(f func(handle rri.DenicHandle) *rri.Query) commandline.ExecCommandHandler {
return func(args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing handle")
}
handle, err := rri.ParseDenicHandle(args[0])
if err != nil {
return err
}
_, err = processQuery(f(handle))
if histHandles != nil {
histHandles.Put(args[0])
}
return err
}
}
func registerCustomCommand(cle *commandline.Environment, cmd customCommand) {
clArgs := make([]commandline.ArgCompletion, 0)
for _, arg := range cmd.Args {
switch strings.ToLower(arg.Type) {
case "domain":
clArgs = append(clArgs, histDomains)
}
}
cle.RegisterCommand(commandline.NewCustomCommand(cmd.Cmd, commandline.NewFixedArgCompletion(clArgs...), func(args []string) error {
fields := rri.NewQueryFieldList()
argIndex := 0
for _, arg := range cmd.Args {
var inValue string
if arg.IsInputParameter() {
if len(args) <= argIndex {
return fmt.Errorf("missing argument '%s'", arg.Name)
}
inValue = args[argIndex]
argIndex++
}
switch strings.ToLower(arg.Type) {
case "domain":
fields.Add(rri.QueryFieldName(arg.Field), inValue)
case "const":
fields.Add(rri.QueryFieldName(arg.Field), arg.Value)
}
}
//TODO fill history
query := rri.NewQuery(rri.LatestVersion, rri.QueryAction(cmd.Action), fields)
_, err := processQuery(query)
return err
}))
}
func cmdLogin(args []string) error {
var pass string
if len(args) < 1 {
return fmt.Errorf("missing RRI username and password")
} else if len(args) < 2 {
console.Println("Enter RRI password:")
console.Print("> ")
var err error
pass, err = console.ReadPassword()
if err != nil {
return err
}
} else {
pass = args[1]
}
if cleRRIClient.IsLoggedIn() {
console.Println("Exiting current session")
cleRRIClient.Logout()
}
return cleRRIClient.Login(args[0], pass)
}
func cmdLogout(args []string) error {
return cleRRIClient.Logout()
}
func cmdCreateHandle(args []string) error {
handle, contactData, err := readContactData(args, 0)
if err != nil {
return err
}
_, err = processQuery(rri.NewCreateContactQuery(handle, contactData))
histHandles.Put(handle.String())
return err
}
func cmdCreateDomain(args []string) error {
domainName, domainData, err := readDomainData(args, 1)
if err != nil {
return err
}
_, err = processQuery(rri.NewCreateDomainQuery(domainName, domainData))
histDomains.Put(domainName)
return err
}
func cmdUpdateDomain(args []string) error {
domainName, domainData, err := readDomainData(args, 1)
if err != nil {
return err
}
//TODO use old domain values for empty fields -> only change explicitly entered data
_, err = processQuery(rri.NewUpdateDomainQuery(domainName, domainData))
histDomains.Put(domainName)
return err
}
func cmdChangeHolder(args []string) error {
domainName, domainData, err := readDomainData(args, 1)
if err != nil {
return err
}
//TODO use old domain values for empty fields -> only change explicitly entered data
_, err = processQuery(rri.NewChangeHolderQuery(domainName, domainData))
histDomains.Put(domainName)
return err
}
func cmdTransit(args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing domain name")
}
domainName := args[0]
var disconnect bool
if len(args) > 1 {
switch strings.ToLower(args[1]) {
case "disconnect":
disconnect = true
case "connect":
disconnect = false
default:
return fmt.Errorf("invalid mode '%s'. expecting 'disconnect' or 'connect'", args[1])
}
}
_, err := processQuery(rri.NewTransitDomainQuery(domainName, disconnect))
histDomains.Put(domainName)
return err
}
func cmdCreateAuthInfo1(args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing domain name")
}
if len(args) < 2 {
return fmt.Errorf("missing auth info secret")
}
var expire time.Time
if len(args) >= 3 {
var err error
expire, err = time.ParseInLocation("2006-01-02", args[2], time.Local)
if err != nil {
expire, err = time.ParseInLocation("20060102", args[2], time.Local)
if err != nil {
return fmt.Errorf("expiration date must be in format yyyy-mm-dd or yyyymmdd")
}
}
} else {
expire = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()+7, 0, 0, 0, 0, time.Local)
console.Println("using default expiration of 1 week")
}
_, err := processQuery(rri.NewCreateAuthInfo1Query(args[0], args[1], expire))
return err
}
func cmdChangeProvider(args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing domain name")
}
if len(args) < 2 {
return fmt.Errorf("missing auth info secret")
}
domainName, domainData, err := readDomainData(args, 2)
if err != nil {
return err
}
_, err = processQuery(rri.NewChangeProviderQuery(domainName, args[1], domainData))
histDomains.Put(domainName)
return err
}
func cmdQueueRead(args []string) error {
_, err := processQuery(rri.NewQueueReadQuery(""))
return err
}
func cmdQueueDelete(args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing message id")
}
_, err := processQuery(rri.NewQueueDeleteQuery(args[0], ""))
return err
}
func cmdRaw(args []string) error {
var rawCommand string
if len(args) > 0 {
rawCommand = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(args[0], "\\n", "\n"), "\\r", "\r"), "\\\\", "\\")
} else {
raw, ok, err := input.Text("")
if err != nil {
return err
}
if ok {
rawCommand = raw
}
}
if len(rawCommand) > 0 {
response, err := cleRRIClient.SendRaw(rawCommand)
if err != nil {
return err
}
console.Println(response)
if returnErrorOnFail {
responseObj, err := rri.ParseResponse(response)
if err != nil {
return fmt.Errorf("RRI server returned an invalid response")
}
if !responseObj.IsSuccessful() {
return fmt.Errorf("RRI returned result 'failed'")
}
}
}
return nil
}
func cmdFile(args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing query file")
}
data, err := os.ReadFile(args[0])
if err != nil {
return err
}
queries, err := parseQueries(string(data))
if err != nil {
return err
}
skipAuthQueries := cleRRIClient.IsLoggedIn()
// find queries to execute first
executedQueryCount := 0
hasLoginLogoutQueries := false
for _, query := range queries {
if query.Action() == rri.ActionLogin || query.Action() == rri.ActionLogout {
hasLoginLogoutQueries = true
if !skipAuthQueries {
executedQueryCount++
}
} else {
executedQueryCount++
}
}
if skipAuthQueries && hasLoginLogoutQueries {
//TODO colored orange
console.Println("Currently logged in. Auth queries will be skipped")
}
for _, query := range queries {
if skipAuthQueries {
if query.Action() == rri.ActionLogin || query.Action() == rri.ActionLogout {
// skip authorization queries
console.Println("Skip query", query) //TODO colored orange
continue
}
}
//TODO colored in send color
console.Println("Exec query", query)
success, err := processQuery(query)
if err != nil {
return err
}
if !success {
break
}
}
return nil
}
func cmdXML(args []string) error {
cleRRIClient.XMLMode = !cleRRIClient.XMLMode
if cleRRIClient.XMLMode {
console.Println("XML mode on")
} else {
console.Println("XML mode off")
}
return nil
}
func cmdVerbose(args []string) error {
if cleRRIClient.RawQueryPrinter == nil {
cleRRIClient.RawQueryPrinter = rawQueryPrinter
cleRRIClient.InnerErrorPrinter = errorPrinter
console.Println("Verbose mode on")
} else {
cleRRIClient.RawQueryPrinter = nil
cleRRIClient.InnerErrorPrinter = nil
console.Println("Verbose mode off")
}
return nil
}
func rawQueryPrinter(msg string, isOutgoing bool) {
if isOutgoing {
console.Printlnf("%s %s%q%s", signSend, colorSendRaw, rri.CensorRawMessage(msg), colorEnd)
} else {
console.Printlnf("%s %s%q%s", signReceive, colorReceiveRaw, rri.CensorRawMessage(msg), colorEnd)
}
}
func errorPrinter(err error) {
console.Printlnf("%sERR: %s%s", colorInnerError, err.Error(), colorEnd)
}
func processQuery(query *rri.Query) (bool, error) {
rawResponse, err := cleRRIClient.SendRaw(query.EncodeKV())
if err != nil {
return false, err
}
response, err := rri.ParseResponse(rawResponse)
if err != nil {
return false, err
}
var colorStr string
if response.IsSuccessful() {
colorStr = colorSuccessResponse
} else {
colorStr = colorErrorResponseMessage
}
console.Print(colorStr)
console.Println(rawResponse)
console.Print(colorEnd)
if returnErrorOnFail && !response.IsSuccessful() {
return false, fmt.Errorf("RRI returned result 'failed'")
}
return response.IsSuccessful(), nil
}
func readDomainData(args []string, dataOffset int) (string, rri.DomainData, error) {
if len(args) < 1 {
return "", rri.DomainData{}, fmt.Errorf("missing domain name")
}
domainName := args[0]
if !strings.HasSuffix(strings.ToLower(domainName), ".de") {
return "", rri.DomainData{}, fmt.Errorf("domain name must end with .de")
}
handleNames := []string{"Holder", "GeneralRequest", "AbuseContact"}
handles := make([]rri.DenicHandle, len(handleNames))
for i := 0; i < len(handleNames); i++ {
if len(args) >= (i + dataOffset + 1) {
handle, err := rri.ParseDenicHandle(args[i+dataOffset])
if err != nil {
return "", rri.DomainData{}, fmt.Errorf("%q: %s", args[i+dataOffset], err.Error())
}
handles[i] = rri.DenicHandle(handle)
} else {
console.Printf("%s Handle> ", handleNames[i])
str, err := commandline.ReadLineWithHistory(histHandles)
if err != nil {
return "", rri.DomainData{}, err
}
handle, err := rri.ParseDenicHandle(str)
if err != nil {
return "", rri.DomainData{}, fmt.Errorf("%q: %s", str, err.Error())
}
histHandles.Put(str)
handles[i] = rri.DenicHandle(handle)
}
}
var nameServers []string
if len(args) >= (dataOffset + len(handleNames)) {
nameServers = args[dataOffset+len(handleNames):]
} else {
for {
console.Printf("NameServer> ")
str, err := console.ReadLine()
if err != nil {
return "", rri.DomainData{}, err
}
if len(str) == 0 {
break
}
nameServers = append(nameServers, str)
}
}
return domainName, rri.DomainData{
HolderHandles: []rri.DenicHandle{handles[0]},
GeneralRequestHandles: []rri.DenicHandle{handles[1]},
AbuseContactHandles: []rri.DenicHandle{handles[2]},
NameServers: nameServers,
}, nil
}