備忘録

備忘録

UbuntuでSOCKSサーバ(Dante)を構築する方法

Ⅰ. はじめに

タイトルの通り「UbuntuでSOCKSサーバ(Dante)を構築する方法」です。

CentOSでSOCKSサーバ(Dante)を構築する方法はこちら
https://kagasu.hatenablog.com/entry/2017/08/26/173052

Ⅱ. 手順(認証なし)

1. Danteをインストールする
apt install dante-server
2. 設定ファイルを編集する

※以下の内容は認証なしで全てのアクセスを許可します。
セキュリティに問題がある為適切な設定を行って下さい。
/etc/danted.conf

$ vim /etc/danted.conf
internal: 0.0.0.0 port = 1080
external: eth0
socksmethod: none
clientmethod: none

client pass {
  from: 0.0.0.0/0 to: 0.0.0.0/0
  # log: connect disconnect
}

socks pass {
  from: 0.0.0.0/0 to: 0.0.0.0/0
}
3. Danteを起動する
systemctl enable danted
systemctl restart danted
# systemctl status danted
4. 接続テストする
$ curl --socks5-hostname 127.0.0.1:1080 http://api.ipify.org/
x.x.x.x

Ⅲ. 手順(ユーザー認証あり)

1. Danteをインストールする
apt install dante-server
2. 設定ファイルを編集する

※以下設定の場合/etc/shadowにパスワードが設定されているユーザーがログイン可能になります(rootを含む)
/etc/danted.conf

logoutput: stderr
internal: 0.0.0.0 port = 1080
external: eth0
clientmethod: none
socksmethod: username
user.privileged: root
user.unprivileged: nobody
user.libwrap: nobody
compatibility: sameport

client pass {
  from: 0.0.0.0/0 to: 0.0.0.0/0
  # log: connect disconnect
}

socks pass {
  from: 0.0.0.0/0 to: 0.0.0.0/0
}
3. Dantedを起動する
systemctl enable danted
systemctl restart danted
4. ユーザーを追加する
adduser --shell /sbin/nologin --gecos "" user01
echo 'user01:pass1234' | chpasswd
5. 接続テストする
$ curl http://api.ipify.org -x socks5://user01:pass1234@127.0.0.1:1080
x.x.x.x

HttpClientFactoryとPollyをコンソールアプリケーションで使用する方法

Ⅱ. やり方

1. 必要なパッケージをインストールする
Install-Package Microsoft.Extensions.Http.Polly
2. サンプルプログラムを書く
using Microsoft.Extensions.DependencyInjection;
using Polly;
using Polly.Extensions.Http;
using Polly.Timeout;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Test
{
  class Program
  {
    static async Task Main()
    {
      const string HttpClientName = "MyHttpClient";

      // HttpClientを生成する
      var policy = HttpPolicyExtensions
        .HandleTransientHttpError()
        .OrResult(msg => msg.StatusCode != HttpStatusCode.OK)
        .Or<TimeoutRejectedException>()
        .WaitAndRetryAsync(10, _ => TimeSpan.FromSeconds(1));

      var timeoutPolicy = Policy
        .TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(10), TimeoutStrategy.Pessimistic);

      var services = new ServiceCollection();
      services
        .AddHttpClient(HttpClientName, x =>
        {
          // Pollyで制御するのでHttpClientのTimeoutは無制限に設定する
          // https://devadjust.exblog.jp/29756261/
          x.Timeout = Timeout.InfiniteTimeSpan;
        })
        .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
        {
          // Proxy = proxy
        })
        .AddPolicyHandler(policy)
        .AddPolicyHandler(timeoutPolicy);

      var provider = services.BuildServiceProvider();
      var factory = provider.GetRequiredService<IHttpClientFactory>();
      var client = factory.CreateClient(HttpClientName);

      var str = await client.GetStringAsync("http://example.com");
      Console.WriteLine(str);
    }
  }
}

実行結果

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
以下省略

Playwrightを利用してWebページのスクリーンショットを撮る方法

Ⅰ. はじめに

タイトルの通り「Playwrightを利用してWebページのスクリーンショットを撮る方法」です。

Ⅱ. やり方

1. Playwrightをインストールする
npm i -D playwright
2. サンプルプログラムを書く
const playwright = require('playwright');

(async () => {
  for (const browserType of ['chromium', 'firefox', 'webkit']) {
    const browser = await playwright[browserType].launch()
    const context = await browser.newContext()
    const page = await context.newPage()
    await page.goto('http://example.com')
    await page.screenshot({ path: `images/${browserType}.png` })
    await browser.close()
  }
})()

実行結果


C#で正規表現を利用してランダム文字列を生成する方法

Ⅰ. はじめに

タイトルの通り「C#正規表現を利用してランダム文字列を生成する方法」です。

Ⅱ. やり方

1. 必要なパッケージをインストールする
Install-Package Fare
2. サンプルプログラムを書く
using Fare;

var pattern = "[a-z0-9]{6}";
var xeger = new Xeger(pattern, new Random());

foreach (var _ in Enumerable.Range(0, 5))
{
    Console.WriteLine(xeger.Generate());
}

実行結果

u9svbp
5a2p9t
r67633
w7h0h1
61n8ig

Muninを利用してサーバをモニタリングする方法

Ⅰ. はじめに

タイトルの通り「Muninを利用してサーバをモニタリングする方法」です。

Ⅱ. 環境

Ⅲ. やり方

(1) Webサーバ側

1. Munin をインストールする
apt install munin
2. Muninの設定を行う
$ vim /etc/munin/munin.conf
htmldir /var/www/html/munin

[server001]
  address 192.168.0.2
  use_node_name yes

[server002]
  address 192.168.0.3
  use_node_name yes
3. Nginxをインストールする
apt install nginx
4. Nginxの設定を行う
mkdir -p /var/www/html/munin
chown -R munin:munin /var/www/html/munin
$ htpasswd -c /etc/nginx/conf.d/.htpasswd_munin admin
$ rm /etc/nginx/sites-enabled/default
$ vim /etc/nginx/conf.d/munin.conf
server {
  listen 80;
  server_name _;
  root /var/www/html/munin;
  index index.html;

  location / {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/conf.d/.htpasswd_munin;
  }
}
5. nginxを起動する
systemctl enable nginx
systemctl restart nginx

(2) 監視される側

1. Munin nodeをインストールする
apt install munin-node
2. Munin nodeの設定を行う
$ vim /etc/munin/munin-node.conf
port 4949
host *
allow ^192\.168\.0\.1$
# ディスクログを取らない
mv /etc/munin/plugins/diskstats /etc/munin/plugins/diskstats.bak
# inodeのログを取らない
mv /etc/munin/plugins/df_inode /etc/munin/plugins/df_inode.bak
# muninのログを取らない
mv /etc/munin/plugins/munin_stats /etc/munin/plugins/munin_stats.bak
# entropyのログを取らない
mv /etc/munin/plugins/entropy /etc/munin/plugins/entropy.bak
# forkのログを取らない
mv /etc/munin/plugins/forks /etc/munin/plugins/forks.bak
# fw_packetsのログを取らない
mv /etc/munin/plugins/fw_packets /etc/munin/plugins/fw_packets.bak
# if_err_eth0のログを取らない
mv /etc/munin/plugins/if_err_eth0 /etc/munin/plugins/if_err_eth0.bak
# irqstatsのログを取らない
mv /etc/munin/plugins/irqstats /etc/munin/plugins/irqstats.bak
# loadのログを取らない
mv /etc/munin/plugins/load /etc/munin/plugins/load.bak
# open_filesのログを取らない
mv /etc/munin/plugins/open_files /etc/munin/plugins/open_files.bak
# open_inodesのログを取らない
mv /etc/munin/plugins/open_inodes /etc/munin/plugins/open_inodes.bak
# processesのログを取らない
mv /etc/munin/plugins/processes /etc/munin/plugins/processes.bak
# proc_priのログを取らない
mv /etc/munin/plugins/proc_pri /etc/munin/plugins/proc_pri.bak
# usersのログを取らない
mv /etc/munin/plugins/users /etc/munin/plugins/users.bak
# vmstatのログを取らない
mv /etc/munin/plugins/vmstat /etc/munin/plugins/vmstat.bak
3. Munin nodeを起動する
systemctl enable munin-node
systemctl restart munin-node

実行結果

f:id:kagasu:20200924105622p:plain

FAQ

Q. モニタリングの間隔は?

A. デフォルト設定で5分ごとです。

Q. Muninの起動設定は?

A. Muninインストール時にmuninユーザのcronに自動登録される為不要です。

Q. 今すぐデータを取得したい

A. 以下コマンドで可能です。

su - munin --shell=/usr/bin/munin-cron