1+ using Renci . SshNet ;
2+ using System . IO ;
3+
4+ using System ;
5+ using System . Collections . Generic ;
6+ using System . Linq ;
7+ using System . Text ;
8+ using System . Threading . Tasks ;
9+
10+ namespace SWSH {
11+ class Program {
12+ static string _command = "" , _version = "pre-alpha-0.0.1" , _mainDirectory = "swsh-data/" ;
13+ static void Main ( string [ ] args ) {
14+ __version ( ) ;
15+ Console . Write ( "swsh --help or -h for help.\n \n " ) ;
16+ __start ( ) ;
17+ }
18+ public static void __start ( ) {
19+ while ( true ) {
20+ __color ( "swsh> " , ConsoleColor . DarkGray ) ;
21+ _command = Console . ReadLine ( ) ;
22+ if ( _command . StartsWith ( "swsh" ) ) {
23+ _command = _command . Replace ( "swsh" , "" ) . Trim ( ) ;
24+ if ( _command == "--version" || _command == "-v" ) __version ( ) ;
25+ else if ( _command == "--add" || _command == "-a" ) {
26+ __color ( "exit" , ConsoleColor . Red ) ;
27+ Console . Write ( " or " ) ;
28+ __color ( "-e" , ConsoleColor . Red ) ;
29+ Console . Write ( " to cancel.\n " ) ;
30+ Console . Write ( "Path to private key: " ) ;
31+ var key = Console . ReadLine ( ) ;
32+ __checkexit ( key ) ;
33+ Console . Write ( "Username: " ) ;
34+ var usr = Console . ReadLine ( ) ;
35+ __checkexit ( usr ) ;
36+ Console . Write ( "Server: " ) ;
37+ var svr = Console . ReadLine ( ) ;
38+ __checkexit ( svr ) ;
39+ getNick :
40+ Console . Write ( "Unique Nickname: " ) ;
41+ var nkn = Console . ReadLine ( ) ;
42+ if ( File . Exists ( _mainDirectory + nkn + ".swsh" ) ) {
43+ __color ( "ERROR: " , ConsoleColor . Red ) ;
44+ Console . WriteLine ( "SWSH -> {0} -> nickname exists" , nkn ) ;
45+ goto getNick ;
46+ }
47+ if ( nkn . Trim ( ) == string . Empty ) goto getNick ;
48+ String [ ] data = new String [ ] { key , usr , svr } ;
49+ if ( ! Directory . Exists ( _mainDirectory ) ) Directory . CreateDirectory ( _mainDirectory ) ;
50+ File . WriteAllLines ( _mainDirectory + nkn + ".swsh" , data ) ;
51+ } else if ( _command == "--help" || _command == "-h" ) {
52+ Console . WriteLine ( "--version -v: -Check the version of swsh." ) ;
53+ Console . WriteLine ( "--add -a: -Add a new connection." ) ;
54+ Console . WriteLine ( "--connect [nickname] -c [nickname]: -Connects to Server over SSH." ) ;
55+ Console . WriteLine ( "--help -h: -Displays this help." ) ;
56+ Console . WriteLine ( "exit: Exits." ) ;
57+ Console . WriteLine ( "\n \n NOTES:\n [1] cd .. is not supported." ) ;
58+ } else if ( _command . StartsWith ( "--connect" ) || _command . StartsWith ( "-c" ) ) {
59+ #region SSH Control
60+ var ccinfo = ( _command . StartsWith ( "--connect" ) ) ? __CreateConnectionInfo ( _command . Remove ( 0 , 10 ) ) : __CreateConnectionInfo ( _command . Remove ( 0 , 3 ) ) ;
61+ if ( ccinfo != null ) {
62+ Console . Write ( "Waiting for response from {0}@{1}...\n " , ccinfo . Username , ccinfo . Host ) ;
63+ using ( var ssh = new SshClient ( ccinfo ) ) {
64+ ssh . Connect ( ) ;
65+ __color ( "Connected to " + ccinfo . Username + "@" + ccinfo . Host + "...\n " , ConsoleColor . Green ) ;
66+ string pwd = " " , home = "" ;
67+ home = pwd = ssh . CreateCommand ( "echo $HOME" ) . Execute ( ) ;
68+ while ( true ) {
69+ if ( pwd == home ) pwd = "~" ;
70+ __color ( pwd , ConsoleColor . Green ) ;
71+ Console . Write ( ":/ $ " ) ;
72+ _command = Console . ReadLine ( ) ;
73+ if ( _command == "exit" )
74+ break ;
75+ else if ( _command . StartsWith ( "cd " ) ) {
76+ _command = _command . Remove ( 0 , 3 ) ;
77+ if ( _command . StartsWith ( "/" ) ) pwd = _command ;
78+ else if ( _command . StartsWith ( "./" ) ) pwd += "/" + _command . Remove ( 0 , 2 ) ;
79+ else if ( _command . StartsWith ( ".." ) ) {
80+ __color ( "ERROR: " , ConsoleColor . Red ) ;
81+ Console . Write ( "SWSH -> cd {0} -> Operation not allowed. Run \" swsh -h\" and see Note #1.\n " , _command ) ;
82+ } else pwd += "/" + _command ;
83+ } else if ( _command == "clear" ) Console . Clear ( ) ;
84+ else {
85+ var result = ssh . CreateCommand ( "cd " + pwd + "; " + _command ) . Execute ( ) ;
86+ Console . Write ( result ) ;
87+ }
88+ }
89+ ssh . Disconnect ( ) ;
90+ }
91+ __color ( "Connection to " + ccinfo . Username + "@" + ccinfo . Host + ", closed.\n " , ConsoleColor . Yellow ) ;
92+ } else break ;
93+ #endregion
94+ }
95+ } else if ( _command == "exit" ) break ;
96+ }
97+ }
98+ public static void __version ( ) {
99+ Console . Write ( " ______ _______ __ __\n / ___/ | / / ___// / / /\n \\ __ \\ | | /| / /\\ __ \\ / /_/ / \n ___/ /| |/ |/ /___/ / __ / \n /____/ |__/|__//____/_/ /_/ \n Secure Windows Shell \n " ) ;
100+ Console . Write ( "\n Release: {0}\n {1}" , _version , "(c) Muhammad Muzzammil & Nabeel Omer\n " ) ;
101+ }
102+ public static void __color ( string message , ConsoleColor cc ) {
103+ Console . ForegroundColor = cc ;
104+ Console . Write ( message ) ;
105+ Console . ResetColor ( ) ;
106+ }
107+ public static void __checkexit ( string keyword ) {
108+ if ( keyword == "exit" || keyword == "-e" ) __start ( ) ;
109+ }
110+ public static ConnectionInfo __CreateConnectionInfo ( string nickname ) {
111+ if ( File . Exists ( _mainDirectory + nickname + ".swsh" ) ) {
112+ string privateKeyFilePath = File . ReadAllLines ( _mainDirectory + nickname + ".swsh" ) [ 0 ] ,
113+ user = File . ReadAllLines ( _mainDirectory + nickname + ".swsh" ) [ 1 ] ,
114+ server = File . ReadAllLines ( _mainDirectory + nickname + ".swsh" ) [ 2 ] ;
115+ ConnectionInfo connectionInfo ;
116+ using ( var stream = new FileStream ( privateKeyFilePath , FileMode . Open , FileAccess . Read ) ) {
117+ var privateKeyFile = new PrivateKeyFile ( stream ) ;
118+ AuthenticationMethod authenticationMethod = new PrivateKeyAuthenticationMethod ( user , privateKeyFile ) ;
119+ connectionInfo = new ConnectionInfo ( server , user , authenticationMethod ) ;
120+ }
121+ return connectionInfo ;
122+ } else {
123+ __color ( "ERROR: " , ConsoleColor . Red ) ;
124+ Console . WriteLine ( "SWSH -> {0} -> nickname does not exists" , nickname ) ;
125+ __start ( ) ;
126+ return null ;
127+ }
128+ }
129+ }
130+ }
0 commit comments