반응형

환경 : 

     client ; windows 10 pc

     server ; 네이버 클라우드 무료서버 ubuntu 18.04 LTS

 

참고 ; medium.com/good-robot/use-visual-studio-code-remote-ssh-sftp-without-crashing-your-server-a1dc2ef0936d

 

 

상황 ; visual studio code 로 원격서버에 ssh 로 접속하여, 작업중에 갑자기 연결끊어졌다. 다시 연결하겠느냐는 메세지 나오면서 먹통되는 현상 발생.

 

해결책 ; 아래와 같이 TypeScript and JavaScript Language FeaturesDisable 시키고 다시 시작하면 잘됨.

 

 

 

 

 

 

반응형
Posted by 자유프로그램
,
반응형

참고 : https://code.visualstudio.com/docs/remote/ssh

 

환경 :  client : 윈도우 10 pc,  visual studio code

         server : 네이버 클라우드 플랫폼 무료 서버 ( Ubuntu 18.04 LTS )

 

 

(1) windows 10 pc 에서, OpenSSH 클라이언트 설치하기 -- client

 

참고 ; docs.microsoft.com/ko-kr/windows-server/administration/openssh/openssh_install_firstuse#installing-openssh-from-the-settings-ui-on-windows-server-2019-or-windows-10-1809

 

 

 

  --> 현재의 본인 PC 에는, OpenSSH 클라이언트가 이미 설치되어있는 상황.

 

(2) 서버에 openssh-server 설치하기. 

      -- 네이버 클라우드 플랫폼의 무료서버 OS 경우, ubuntu 18.04 LTS 서버에는 기본 설치 되어있음.

 

(3) visual studio code 의 extension 중에서, Remote-SSH 설치하기

 

설치후, visual studio code 좌측 하단에 remote window 열수있는 icon 생김.

 

 

 

<<  원격 서버에 접속하기 >>

 

(1)  visual studio code 좌측하단 >< 아이콘 누른후,

      --> command palette 나오면, Remote-SSH: Connect to Host... 을 선택한다.

연결한 원격서버주소와 접속 id를 입력한다.

-- 형식 ;  username@host

               ex) tester@123.12.13.55  ( 서버ip = 123.12.13.55, 사용자명 = tester 인 경우)

 

--> 이후에는, 접속할 서버의 OS 를 선택하라고 물어봄.

        ; 접속할 서버가 Ubuntu 18.04 이므로, Linux 선택하면됨.

 

--> 이후에, 사용자의 서버접속에 필요한 password 입력해야함.

 

--연결된후,  File 메뉴에서 Open Folder 선택.

 

-->  서버의 폴더를 선택하는 화면 나옴.

 

 

==> 이제는 서버에 있는 파일을 직접 편집할수있다.

     --> 서버 편집시 nodemon 도 잘 작동한다.

            &&  터미널도 서버의 터미널이 열린다.. ok!!!

 

 

반응형
Posted by 자유프로그램
,
반응형

nodejs -- http -> https 변환 ( redirect )



환경 : 네이버 클라우드 플랫폼 무료 서버, Ubuntu 18.04 LTS, noce.js v14.15.3



http 로 접속하면, https 주소로 redirect 시키자




http -- 80 포트 사용
https -- 443 포트 사용




** Express 에서, http & https 를 구별하는 법 3가지
(1) req.secure
(2) req.protocol
(3) req.headers['X-Forwarded-Proto']



** 방법 1. -- req.protocol 또는 req.headers["X-Forwarded-Proto"] 이용

app.get("*", (reqresnext=> {
    console.log("middleware sercure app2 ==> " + req.headers['X-Forwarded-Proto']);
    console.log("req.protocol == " + req.protocol);

    let protocol = req.headers['X-Forwarded-Proto'] || req.protocol;
    console.log("protocol == " + protocol);
    
    if(protocol == 'http'){
        let to = "https://" + req.headers.host + req.url;
        console.log("to ==> " + to);

        return res.redirect(to);
    }
    next();
})


** 방법2. -- req.secure 이용

app.get("*", (reqresnext=> {
    console.log("req.secure == " + req.secure);
    
    if(req.secure){
        // --- https
        next();
    }else{
        // -- http
        let to = "https://" + req.headers.host + req.url;
        console.log("to ==> " + to);

        return res.redirect("https://" + req.headers.host + req.url);
    }
})













반응형
Posted by 자유프로그램
,
반응형

C# -- HttpClient 기본 사용법



** https, http 모두 상관없이 사용가능함.



** Main() 함수에서는 await 를 사용못함

   --> 따라서, Main() 함수내에서, 비동기함수를 동기적으로 실행시키기위해서는 .GetAwaiter().GetResult() 를 사용해야함.



using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace HttpClient_test
{
    class Program
    {
        static readonly HttpClient httpClient = new HttpClient();

        //static HttpRequestHeaders requestHeaders = httpClient.DefaultRequestHeaders;

        static void Main(string[] args)
        {

            string httpsUrl = "https://jsonplaceholder.typicode.com/todos/1"
            string httpUrl = "http://jsonplaceholder.typicode.com/todos/2";

            Console.WriteLine($" -------- HTTPS ------------");

            Test(httpsUrl).GetAwaiter().GetResult();  // Main함수에서 await Test(httpsUrl) 사용못하므로, 이를 대신함

            Console.WriteLine($"\n\n\n --------- HTTP ------------");

            Test(httpUrl).GetAwaiter().GetResult();

            Console.WriteLine($" ---------- END ------------");
            Console.Read();
        }

        static async Task Test(string url)
        {
            try
            {
                using (var response = await httpClient.GetAsync(url))
                {
                    Console.WriteLine(response.StatusCode);

                    if (HttpStatusCode.OK == response.StatusCode)
                    {
                        string body = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(body);
                    }
                    else
                    {
                        Console.WriteLine($" -- response.ReasonPhrase ==> {response.ReasonPhrase}");
                    }
                }

            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine($"ex.Message={ex.Message}");
                Console.WriteLine($"ex.InnerException.Message = {ex.InnerException.Message}");

                Console.WriteLine($"----------- 서버에 연결할수없습니다 ---------------------");
            }
            catch (Exception ex2)
            {
                Console.WriteLine($"Exception={ex2.Message}");
            }
        }
    }
}




** 실행결과






반응형
Posted by 자유프로그램
,
반응형

nodejs -- https, http 서버 만들기


서버 환경 : Let's Encrypt 인증서 설치한 네이버 클라우드 플랫폼 무료 서버, Ubuntu 18.04 LTS OS 사용.



** 주의; Let's Encrypt 인증서 설치한, 경로는 본인의 인증서 설치 경로로 바꾸어 사용하자.


** Express 에서 다중 포트 서버 만들자.

    -- 80 포트는 http 서버

    -- 3000 포트는 https 서버


'use strict';
const fs = require("fs");
const http = require("http");
const https = require("https");
const express = require("express");

const app = express();  // https
const app2 = express();  // http

// yourdomain.com 은 실제로 사용중인 도메인으로 바꿔야함. -- Let's Encrypt 인증서 설치시 위치 사용.
const privateKey = fs.readFileSync("/etc/letsencrypt/live/yourdomain.com/privkey.pem", "utf8");
const certificate = fs.readFileSync("/etc/letsencrypt/live/yourdomain.com/cert.pem", "utf8")
const ca = fs.readFileSync("/etc/letsencrypt/live/yourdomain.com/chain.pem", "utf8")

const credentials = {
    key: privateKey,
    cert: certificate,
    ca: ca
};

const httpServer = http.createServer(app2);
const httpsServer = https.createServer(credentials, app);

// 80 port -- http
app2.get("/"(reqres) => {
    console.log("------ http get / -----" + (new Date()).toLocaleString());
    console.log("req.ip => " + req.ip);
    console.log("req.hostname => " + req.hostname);
    console.log(req.url);
    console.log(req.originalUrl);

    res.send("<h1>HTTP Server running on port 80</h1>");
})

// 3000 port -- https
app.get("/"(reqres) => {
    console.log("------ https get / -----" + (new Date()).toLocaleString());
    console.log("req.ip => " + req.ip);
    console.log("req.hostname => " + req.hostname);
    console.log(req.url);
    console.log(req.originalUrl);

    res.send("<h1>HTTPS Server running on port 3000</h1>");
})


httpServer.listen(80() => {
    console.log((new Date()).toLocaleString());
    console.log('HTTP Server running on port 80');
})

httpsServer.listen(3000()=>{
    console.log((new Date()).toLocaleString());
    console.log(`HTTPS -- listening on port 3000 ...`);
})



** node 로 실행한 화면


$ sudo node app.js





** local 로 설치한, nodemon 으로 실행한 화면


$ sudo npx nodemon app.js







** browser 에서 접속 성공한 화면








반응형
Posted by 자유프로그램
,