35
35
// SshproxyVersion is set by Makefile
36
36
SshproxyVersion = "0.0.0+noproperlybuilt"
37
37
defaultConfig = "/etc/sshproxy/sshproxy.yaml"
38
- defaultHostPort = "22"
39
38
)
40
39
41
40
func mustInitEtcdClient (configFile string ) * utils.Client {
@@ -536,6 +535,13 @@ func setErrorBanner(errorBanner string, expire time.Time, configFile string) err
536
535
return cli .SetErrorBanner (errorBanner , expire )
537
536
}
538
537
538
+ func delErrorBanner (configFile string ) error {
539
+ cli := mustInitEtcdClient (configFile )
540
+ defer cli .Close ()
541
+
542
+ return cli .DelErrorBanner ()
543
+ }
544
+
539
545
func showErrorBanner (configFile string ) {
540
546
cli := mustInitEtcdClient (configFile )
541
547
defer cli .Close ()
@@ -592,7 +598,7 @@ The commands are:
592
598
version show version number and exit
593
599
show show states present in etcd
594
600
enable enable a host in etcd
595
- forget forget a host in etcd
601
+ forget forget a host/error_banner in etcd
596
602
disable disable a host in etcd
597
603
error_banner set the error banner in etcd
598
604
@@ -654,39 +660,53 @@ The options are:
654
660
return fs
655
661
}
656
662
657
- func newEnableParser () * flag.FlagSet {
663
+ func newEnableParser (allFlag * bool , hostString * string , portString * string ) * flag.FlagSet {
658
664
fs := flag .NewFlagSet ("enable" , flag .ExitOnError )
665
+ fs .BoolVar (allFlag , "all" , false , "enable all hosts present in config" )
666
+ fs .StringVar (hostString , "host" , "" , "hostname to enable (can be a nodeset)" )
667
+ fs .StringVar (portString , "port" , "" , "port to enable (can be a nodeset)" )
659
668
fs .Usage = func () {
660
- fmt .Fprintf (flag .CommandLine .Output (), `Usage: %s enable HOST [PORT]
669
+ fmt .Fprintf (flag .CommandLine .Output (), `Usage: %s enable -all|-host HOST [-port PORT]
661
670
662
- Enable a previously disabled host in etcd. The default port is %s. Host and port
663
- can be nodesets.
664
- ` , os . Args [ 0 ], defaultHostPort )
671
+ Enable a previously disabled host in etcd.
672
+ ` , os . Args [ 0 ])
673
+ fs . PrintDefaults ( )
665
674
os .Exit (2 )
666
675
}
667
676
return fs
668
677
}
669
678
670
- func newForgetParser () * flag.FlagSet {
679
+ func newForgetParser (allFlag * bool , hostString * string , portString * string ) * flag.FlagSet {
671
680
fs := flag .NewFlagSet ("forget" , flag .ExitOnError )
681
+ fs .BoolVar (allFlag , "all" , false , "forget all hosts present in config" )
682
+ fs .StringVar (hostString , "host" , "" , "hostname to forget (can be a nodeset)" )
683
+ fs .StringVar (portString , "port" , "" , "port to forget (can be a nodeset)" )
672
684
fs .Usage = func () {
673
- fmt .Fprintf (flag .CommandLine .Output (), `Usage: %s forget HOST [PORT ]
685
+ fmt .Fprintf (flag .CommandLine .Output (), `Usage: %s forget COMMAND [OPTIONS ]
674
686
675
- Forget a host in etcd. The default port is %s. Remember that if this host is
676
- used, it will appear back in the list. Host and port can be nodesets.
677
- ` , os .Args [0 ], defaultHostPort )
687
+ The commands are:
688
+ host -all|-host HOST [-port PORT] forget a host in etcd
689
+ error_banner forget the error_banner in etcd
690
+
691
+ The options are:
692
+ ` , os .Args [0 ])
693
+ fs .PrintDefaults ()
678
694
os .Exit (2 )
679
695
}
680
696
return fs
681
697
}
682
698
683
- func newDisableParser () * flag.FlagSet {
699
+ func newDisableParser (allFlag * bool , hostString * string , portString * string ) * flag.FlagSet {
684
700
fs := flag .NewFlagSet ("disable" , flag .ExitOnError )
701
+ fs .BoolVar (allFlag , "all" , false , "disable all hosts present in config" )
702
+ fs .StringVar (hostString , "host" , "" , "hostname to disable (can be a nodeset)" )
703
+ fs .StringVar (portString , "port" , "" , "port to disable (can be a nodeset)" )
685
704
fs .Usage = func () {
686
- fmt .Fprintf (flag .CommandLine .Output (), `Usage: %s disable HOST [PORT]
705
+ fmt .Fprintf (flag .CommandLine .Output (), `Usage: %s disable -all|-host HOST [-port PORT]
687
706
688
- Disable a host in etcd. The default port is %s. Host and port can be nodesets.
689
- ` , os .Args [0 ], defaultHostPort )
707
+ Disable a host in etcd.
708
+ ` , os .Args [0 ])
709
+ fs .PrintDefaults ()
690
710
os .Exit (2 )
691
711
}
692
712
return fs
@@ -708,53 +728,66 @@ The options are:
708
728
return fs
709
729
}
710
730
711
- func getHostPortFromCommandLine (args [] string ) ([] string , []string , error ) {
731
+ func getHostPortFromCommandLine (allFlag bool , hostsNodeset string , portsNodeset string , configFile string ) ( []string , error ) {
712
732
_ , nodesetDlclose , nodesetExpand := nodesets .InitExpander ()
713
733
defer nodesetDlclose ()
714
- hostsNodeset , portsNodeset := "" , defaultHostPort
715
- switch len (args ) {
716
- case 2 :
717
- hostsNodeset , portsNodeset = args [0 ], args [1 ]
718
- case 1 :
719
- hostsNodeset = args [0 ]
720
- default :
721
- return []string {}, []string {}, fmt .Errorf ("wrong number of arguments" )
722
- }
723
734
724
- hosts , err := nodesetExpand ( hostsNodeset )
735
+ configDests , err := utils . LoadAllDestsFromConfig ( configFile )
725
736
if err != nil {
726
- return []string {}, [] string {}, fmt .Errorf ("%s" , err )
737
+ return []string {}, fmt .Errorf ("%s" , err )
727
738
}
728
- ports , err := nodesetExpand (portsNodeset )
729
- if err != nil {
730
- return []string {}, []string {}, fmt .Errorf ("%s" , err )
739
+
740
+ if allFlag && portsNodeset == "" {
741
+ return configDests , nil
742
+ }
743
+
744
+ var hosts []string
745
+ var ports []string
746
+ for _ , configDest := range configDests {
747
+ host , port , err := utils .SplitHostPort (configDest )
748
+ if err != nil {
749
+ return []string {}, fmt .Errorf ("%s" , err )
750
+ }
751
+ hosts = append (hosts , host )
752
+ ports = append (ports , port )
753
+ }
754
+
755
+ if ! allFlag {
756
+ hosts , err = nodesetExpand (hostsNodeset )
757
+ if err != nil {
758
+ return []string {}, fmt .Errorf ("%s" , err )
759
+ }
731
760
}
761
+
762
+ if portsNodeset != "" {
763
+ ports , err = nodesetExpand (portsNodeset )
764
+ if err != nil {
765
+ return []string {}, fmt .Errorf ("%s" , err )
766
+ }
767
+ }
768
+
769
+ var hostPorts []string
732
770
for _ , port := range ports {
733
771
if iport , err := strconv .Atoi (port ); err != nil {
734
- return []string {}, [] string {}, fmt .Errorf ("port \" %s\" must be an integer" , port )
772
+ return []string {}, fmt .Errorf ("port \" %s\" must be an integer" , port )
735
773
} else if iport < 0 || iport > 65535 {
736
- return []string {}, [] string {}, fmt .Errorf ("port \" %s\" must be in the 0-65535 range" , port )
774
+ return []string {}, fmt .Errorf ("port \" %s\" must be in the 0-65535 range" , port )
737
775
}
738
776
for _ , host := range hosts {
739
777
if _ , _ , err := net .SplitHostPort (host + ":" + port ); err != nil {
740
- return []string {}, [] string {}, fmt .Errorf ("%s" , err )
778
+ return []string {}, fmt .Errorf ("%s" , err )
741
779
}
780
+ hostPorts = append (hostPorts , host + ":" + port )
742
781
}
743
782
}
744
- return hosts , ports , nil
783
+ return hostPorts , nil
745
784
}
746
785
747
786
func getErrorBannerFromCommandLine (args []string ) (string , error ) {
748
- errorBanner := ""
749
- switch len (args ) {
750
- case 0 :
751
- errorBanner = ""
752
- case 1 :
753
- errorBanner = args [0 ]
754
- default :
755
- return "" , fmt .Errorf ("wrong number of arguments" )
787
+ if len (args ) == 1 {
788
+ return args [0 ], nil
756
789
}
757
- return errorBanner , nil
790
+ return "" , fmt . Errorf ( "wrong number of arguments" )
758
791
}
759
792
760
793
func byteToHuman (b int , passthrough bool ) string {
@@ -831,14 +864,16 @@ func main() {
831
864
var userString string
832
865
var groupsString string
833
866
var sourceString string
867
+ var hostString string
868
+ var portString string
834
869
835
870
parsers := map [string ]* flag.FlagSet {
836
871
"help" : newHelpParser (),
837
872
"version" : newVersionParser (),
838
873
"show" : newShowParser (& csvFlag , & jsonFlag , & allFlag , & userString , & groupsString , & sourceString ),
839
- "enable" : newEnableParser (),
840
- "forget" : newForgetParser (),
841
- "disable" : newDisableParser (),
874
+ "enable" : newEnableParser (& allFlag , & hostString , & portString ),
875
+ "forget" : newForgetParser (& allFlag , & hostString , & portString ),
876
+ "disable" : newDisableParser (& allFlag , & hostString , & portString ),
842
877
"error_banner" : newErrorBannerParser (& expire ),
843
878
}
844
879
@@ -866,7 +901,7 @@ func main() {
866
901
p := parsers [cmd ]
867
902
p .Parse (args )
868
903
if p .NArg () == 0 {
869
- fmt .Fprintf (os .Stderr , "ERROR: missing 'hosts' or 'connections '\n \n " )
904
+ fmt .Fprintf (os .Stderr , "ERROR: missing 'hosts', 'connections', 'users', 'groups', 'error_banner' or 'config '\n \n " )
870
905
p .Usage ()
871
906
}
872
907
subcmd := p .Arg (0 )
@@ -893,41 +928,75 @@ func main() {
893
928
case "enable" :
894
929
p := parsers [cmd ]
895
930
p .Parse (args )
896
- hosts , ports , err := getHostPortFromCommandLine (p .Args ())
931
+ if ! allFlag && hostString == "" {
932
+ fmt .Fprintf (os .Stderr , "ERROR: missing '-all' or '-host'\n \n " )
933
+ p .Usage ()
934
+ }
935
+ hostPorts , err := getHostPortFromCommandLine (allFlag , hostString , portString , * configFile )
897
936
if err != nil {
898
937
fmt .Fprintf (os .Stderr , "ERROR: %s\n \n " , err )
899
938
p .Usage ()
900
939
}
901
- for _ , host := range hosts {
902
- for _ , port := range ports {
903
- enableHost (host , port , * configFile )
940
+ for _ , hostPort := range hostPorts {
941
+ host , port , err := utils .SplitHostPort (hostPort )
942
+ if err != nil {
943
+ fmt .Fprintf (os .Stderr , "ERROR: %s\n \n " , err )
944
+ p .Usage ()
904
945
}
946
+ enableHost (host , port , * configFile )
905
947
}
906
948
case "forget" :
907
949
p := parsers [cmd ]
908
950
p .Parse (args )
909
- hosts , ports , err := getHostPortFromCommandLine (p .Args ())
910
- if err != nil {
911
- fmt .Fprintf (os .Stderr , "ERROR: %s\n \n " , err )
951
+ if p .NArg () == 0 {
952
+ fmt .Fprintf (os .Stderr , "ERROR: missing 'host' or 'error_banner'\n \n " )
912
953
p .Usage ()
913
954
}
914
- for _ , host := range hosts {
915
- for _ , port := range ports {
955
+ subcmd := p .Arg (0 )
956
+ // parse flags after subcommand
957
+ args = p .Args ()[1 :]
958
+ p .Parse (args )
959
+ switch subcmd {
960
+ case "host" :
961
+ if ! allFlag && hostString == "" {
962
+ fmt .Fprintf (os .Stderr , "ERROR: missing '-all' or '-host'\n \n " )
963
+ p .Usage ()
964
+ }
965
+ hostPorts , err := getHostPortFromCommandLine (allFlag , hostString , portString , * configFile )
966
+ if err != nil {
967
+ fmt .Fprintf (os .Stderr , "ERROR: %s\n \n " , err )
968
+ p .Usage ()
969
+ }
970
+ for _ , hostPort := range hostPorts {
971
+ host , port , err := utils .SplitHostPort (hostPort )
972
+ if err != nil {
973
+ fmt .Fprintf (os .Stderr , "ERROR: %s\n \n " , err )
974
+ p .Usage ()
975
+ }
916
976
forgetHost (host , port , * configFile )
917
977
}
978
+ case "error_banner" :
979
+ delErrorBanner (* configFile )
918
980
}
919
981
case "disable" :
920
982
p := parsers [cmd ]
921
983
p .Parse (args )
922
- hosts , ports , err := getHostPortFromCommandLine (p .Args ())
984
+ if ! allFlag && hostString == "" {
985
+ fmt .Fprintf (os .Stderr , "ERROR: missing '-all' or '-host'\n \n " )
986
+ p .Usage ()
987
+ }
988
+ hostPorts , err := getHostPortFromCommandLine (allFlag , hostString , portString , * configFile )
923
989
if err != nil {
924
990
fmt .Fprintf (os .Stderr , "ERROR: %s\n \n " , err )
925
991
p .Usage ()
926
992
}
927
- for _ , host := range hosts {
928
- for _ , port := range ports {
929
- disableHost (host , port , * configFile )
993
+ for _ , hostPort := range hostPorts {
994
+ host , port , err := utils .SplitHostPort (hostPort )
995
+ if err != nil {
996
+ fmt .Fprintf (os .Stderr , "ERROR: %s\n \n " , err )
997
+ p .Usage ()
930
998
}
999
+ disableHost (host , port , * configFile )
931
1000
}
932
1001
case "error_banner" :
933
1002
p := parsers [cmd ]
0 commit comments