Tunnel SSH Directly To HTTP With GoLang!?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone in this video I'm going to show you I'm going to teach you how you can basically make a tunnel between an SSH connection and an HTTP connection somewhere on the other side of the world in the browser so how is it going to work I'm going to show you right now so we're going to say SSH localhost minus p222 and I'm gonna python basically not python I'm gonna write my main.go file into this SSH connection I'm going to press enter so basically ask me for my fingerprint gonna say yes so what's happening is we're going to generate an ID heat a random number right and I'm going to use this number to connect somebody else in the world directly tunnel it between my SSH and the browser so I'm going to say localhost um localhost 3000 and then I'm going to say ID is going to be this beautiful ID we got we're going to press enter and you see we got the main.go file directly streamed from my SSH connection to the HTTP connection that's what we're gonna build that's what I'm going to teach you right now but before we continue if you're not subscribed to my channel and you appreciate the work that I'm delivering to you consider subscribing give me a thumbs up at least some questions in the comments and of course jump into my Discord Community all the links are down below and for the people that really wanna level up as a gooling engineer I have made the full-time godf program which is a program for people that are willing to become professionally active as a goaling and Journeys over 60 hours of videos uh check it out uh let me know what you think about that and I'm happy to see you as one of my students students all right so basically I already have this project made but it's no problem because we're gonna basically um delete this thing have you gonna rebuild it right so what we're going to do is easily. we're going to run RF this SSH tunnel thingy we're gonna make it again let me boot this phone up real quick for you guys so we're going to say SSH HTTP tunnel heat CD boom just like that uh yeah let's do quotes inside of this project right I'm gonna delete this meme so the first thing we're going to do is we're going to make a main of course here uh yes perfectly fine so I think the best thing we're gonna do is started out assh first right so what's going on here where is my uh let's take this is fine yeah so what we need there is a cool package which is called the glider Labs SSH and uh it basically does everything for us right so we're going to use that so we're gonna copy this uh but the first I think I need to do a go mod uh emit here which is going to be getup.com NDM uh I'm gonna call HTTP tunnel it doesn't really matter press enter the heat then I'm gonna do a go get of an unformatted string here that's no problem we're gonna fix that just like this press enter so it's adding that stuff then of course we're gonna copy um the shenanigans here and then we're gonna paste that in so what this does is basically it's um handles SSH connections as a server right as an SSH server as easy as HTTP right it's very simple um yeah that's fine so we're gonna listen at Port 222 audio wide string that's fine so what we're gonna do first is we need to basically make a tunnel between SSH and HTTP how we're going to do that is by actually making a type which is going to be the type of tunnel and that's going to be a structure right and what do we need well we need a writers which is going to be which is what we need right we're gonna basically send the writer over to our SSH so we can actually copy stuff into the writer and in our case for a HTTP is going to be the response writer right but we're not going to hardcore it as a response right we're gonna just use the interface i o right so you can use it with editing you won't so we're going to say I O writer the next thing we also need is a Dom a Don Channel which is going to be uh something we need to communicate between SSH and S and and the browser while the the HTTP connection that we don't writing what we need to write right a tunnel is done the tunnel is completed so that's going to be a chance directed an empty strict just like this perfectly fine um so what's going to happen is each time somebody connects with SSH it's going to trigger this function right so what we're going to do is we're going to generate a random ID first and you will see why so we're going to say at rent you can generate a string or something else it doesn't really matter we're going to use inst for now because it's easy so we're going to generate a random string uh Matt dot Max and should be fine uh yes we're also going to use a global variable heat so I'm going to say tunnels which is going to be a map of and mapped to Champ tunnel just like that we're going to make it like this very important guys before you Pitchfork me you need to lock this map if you really want to be pinpoint precise you need to load this map I don't think it's needed because there is a big delay between communication but Lock It Anyway use a mutex lock your map it's concurrently unsafe you're gonna get to jail right but we don't have time because your attention span is basically as the same as a mosquito which is completely nothing right so I need to hurry up for the algorithm it's all for the algorithm so basically we have this ID here that's perfectly fine so we're gonna say tunnels ID is going to be make me a Chan of tunnel that's we're gonna do the next thing we're gonna do is basically we are going to say that the tunnel itself which is going to come from http and it will all be clear very soon it's going to be tunnels ID just like that right so now uh so what's going to happen is we're going to make a tunnel are we gonna wait that's what we're gonna do we're gonna wait until the tunnel is established right because we don't know who's coming right um what we also need to do is basically print out our link so we can actually see that and copy it into the banana right so we're going to see a link it's going to this thing or ID actually to be honest tunnel ID is this thing which is going to be the ID perfectly fine then we're gonna also write each that the tunnel is ready just for uh debugging internal is ready right and then what we're gonna do is we're gonna IO copy right so what basically is going to happen is everything you put into your SSH connection every byte you're basically gonna write into your SSH connection you're gonna copy that with a tunnel to your uh well tunnel tunnel whatever you want to call it a pipe it is what it is uh you're gonna copy everything from the SSH connection to the browser that's what we're gonna do so we're gonna copy the tunnel dot writers uh what's going on is of course we cannot do that we need to do this right the tunnel.writed and we are going to copy the S right and the S is basically or as is its session and um because I O Copy actually expects a right a destination which is writer and a reader but the cool stuff is that our session is a reader so we are completely fine right of course you could do something like this uh underscore this device written or something like that and then an error and if the ad is not nil you can lock fatal or something whatever do your error handling check the bytes print that out whatever you want to do with that right so the next thing we need to do is basically each time we are done we need to communicate back to the to the browser that we are actually done copying right so we're gonna say we're going to do that by just closing the tunnel dot done channel right that's what we're gonna do and if we really want we could do s right uh which is basically right to our SSH connection is um we are done something like that yes that seems good that's basically what SSH Handler right the next thing we need is uh handle this in our HTTP Handler right so what we're gonna do real quick is we're gonna say go Funk and the reason why we need to go Funk Heat is because uh you need to understand that basically this uh SSH listen and serve will block right so if we play something below it or or even uh HTTP listener if it's going to block node SSH server it's not going to get booted up right so what we're gonna do is do a go from heat I'm going to say HTTP handle Funk which is going to be slash and then we're going to say handle request which we're gonna doesn't exist yet we're gonna make that and now we're going to say http.lesson and serve we're gonna listen to uh the port of all soydas which is 3000 and then we going to say Mill here because we don't have a handlet maybe we can do a lock fatal here so we know if something is wrong maybe something is already listening to 3000 then you have no clue what's going on you're gonna spend the whole day debugging this application uh so we're going to say funk handle a request which is going to be W HTTP a response response writers or pointer HTTP dot a request and we're going to get the ID which is going to be Arch URL query get you can use any framework you want right you can use Fiber gen tonic whatever you name it it's fine um get we need to ID hitch right that's fine that we're going to say if underscore okay actually to be honest we can get the pipe here tunnel actually is going to be tunnels doesn't need to be a global variable no you can do whatever you wanted it it's just demonstrational purposes I'm using this the same approach and send it and I'm using a global variable sometimes a global variable is pretty defined doesn't really matter Donald's ID right we're gonna check that and actually to be honest we could do that if not okay uh we can actually do something like uh I don't know just Panic or something or write it to the screen I don't even care about that actually to be honest we could do W writes bytes tunnel not found does not exist not found is not found uh return heat pretty fine then we got a tunnel wait what's going on it of course this is an ID string yeah so we're going to say ID we don't care about the error is going to be um string conversion dot a toy this ID is better defined um yes then we have a tunnel and then we're gonna say that we're gonna say that tunnel which is a channel right it's a channel we're going to communicate with a channel we're gonna say uh tunnel is going to be a new tunnel well not the new tunnel it's just internal object strict whatever you want to call it and we're going to say that the W is going to be our response writer here right and then the Dom Channel actually we need to make a teach we're going to say don't it's going to be make a chain of strict empty and then we're gonna say Dom Channel this doesn't exist just like that boom safe so now we have a new tunnel and we're gonna write a tunnel into a channel and then we're gonna just say we're gonna wait don't Channel and we're gonna basically uh block here on the dot channel right because maybe we are copying large files maybe we are doing I don't know maybe you're chatting or I don't I don't know what's going on it doesn't really matter but we need to wait until SSH is done until the we say okay we are done tunneling our data whatever streaming or data it's completely done and then we're gonna close our done Channel and then the HTTP connection will be closed returns right the complete response will be done right uh yes that's that that's fine do we need to do something else that's the question here uh yeah we close this channel here we are done probably yeah I think it's fun we copy clue okay cool fine so uh let's let's test this thing so I'm going to make a new uh two two command uh Terminal C the first thing we're gonna do is go run may not go let's see what's going on here uh no arrows that's fine the next thing we're going to do is uh first of all I need to delete this where is this thing it doesn't matter it doesn't matter let's let's just do it assh localhost minus P which basically means connect me on Port 2222 because 2 2 is your uh is already yeah it's it's your default host right it's it's your default SSH Shenanigans going on on your PC okay and then we can use this thing in heat which is basically uh you're gonna write you're gonna copy whatever you're gonna call it you're gonna write that into uh basically it's Unix you can write it to a file or whatever right but in this case it's going to be an SSH connection and for Simplicity we're going to use main.go and we're going to press enter of course you can see warning remote host identification has changed that's because each time you're gonna boot up your SSH server you are going to create another Hood it's going to create new keys um and a new host Keys which basically means it's a new hosting you didn't add so um well you already added it but it's a different key so it's going to complain how can you fix that well if you go you're not gonna do it right now but I can tell you basically uh there are some configuration you can set that in the SSH server from the package we just installed where you can basically give your own key so each time you reboot it's going to use the same key so you will not have that errors over and over again but a for Simplicity what we can do is just copy this thing blindly copy paste enter press and then we do again the same thing here which is SSH localhost press enter and then a fingerprint yes so now you can see we are blocking right so what SSH is blocking its weight in for the other side right so how do we do that hopefully we have a tunnel ID here which we created we're going to copy this tunnel ID and let's do just Google here so we can see just Google find I'm going to say localhost not quite sure what is 555 is always coming up first it's so annoying heat uh localhost this one we're going to say ID yeah it's fine ID then we're going to paste that in and now we're going to press enter heat and you can see it piped it tunnels uh exactly what we want right so we can also do something else here right we could do um Dental is ready actually did we write completed no but it's fine um wait this is okay this uh uh we are done here you see we are done that's basically uh what happened right so you can do a lot of cool stuff right you could do something like this um can we do this Echo hello world not quite sure if it's gonna work press enter uh no it's not gonna work that's that's sad why does not working anyway so so it doesn't matter you can pipe anything you want in it um in this case uh may not go actually to be honest so you can see it's uh it's created a new ID here and it's going to basically just return the same file right press enter and you can see it's the same file but you can do anything you can transfer files you can um a zip you can do a lot of stuff it's exactly the same thing that how send that works well this is basically it guys I hope um you had something about it I think it's a very cool trick uh the possibilities are basically endless um it uses good channels and all that stuff it uses IO copy uh some cool stuff uh for you to learn from if you like this video consider subscribing to my channel give me a thumbs up and uh leave some questions in the comments jump into the Discord community and like I said like I mentioned in the beginning of the video if you're interested in becoming a full-time go Dev check out fulltimegoodev.com and learn from my 10 years of cooling experience right thanks for watching and I see you in the next video
Info
Channel: Anthony GG
Views: 9,544
Rating: undefined out of 5
Keywords: ssh tutorial, ssh, linux, secure shell, golang, go programming, learn go, vscode, vim vscode, golang course, golang tutorial, go programming language, ssh server, go tutorial, golang ssh, go ssh server, golang tunnel, go lang, secure shell tutorial, ssh keys, golang programming, go programming language tutorial, go tutorial for beginners, go programming tutorial, golang tutorials, coding, software engineering, web development, typescript
Id: RK5xh7xH1Jw
Channel Id: undefined
Length: 17min 16sec (1036 seconds)
Published: Thu Jun 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.