分类 【编程开发】 下的文章

解决golang https请求提示x509: certificate signed by unknown authority

使用golang做一个小爬虫的时候,目标URL是一个使用了自签证书的https站点,在请求时,报错如下:

Get https://x.x.x.x/cgi-bin/showflux.cgi: x509: certificate signed by unknown authority
panic: Get https://x.x.x.x/cgi-bin/showflux.cgi: x509: certificate signed by unknown authority

仅仅为了爬取内容而言,可以使用以下方法解决

步骤一:
引入 "crypto/tls" 包

步骤二:
定义TLSClientConfig,忽略证书校验:

tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }

步骤三:
修改

client := &http.Client{}

client := &http.Client{Transport: tr}

golang环境变量配置

要安装并顺利使用 Go,第一步就是要设置 Go 的环境。

需要设置的 Go 的环境变量,一共有三个。

GOROOT Go 语言的源码以及安装目录。
GOPATH Go 语言的开发目录,目录可以有多个,但是,当我们执行 go get 命令的时候,如未指定目录,会默认保存在第一个目录下。

GOROOT_BOOTSTRAP 这个目录在安装 Go 1.5 版本及之后的版本时需要设置。由于在 1.4 版本后,Go 编译器实现了自举,即通过 1.4 版本来编译安装之后版本的编译器。如果不设置该环境变量的话,会产生这样一个错误 Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.。

除此之外,还需要配置 PATH 环境变量到 Go 的二进制程序目录。
我们需要在 ~/.bash_profile 中添加下面的代码(我把所有的 Go 语言相关的东西都放在了 ~/.golang 下面了):

export GOROOT=$HOME/.golang/go
export GOPATH=$HOME/.golang/path
export PATH=$PATH:$HOME/.golang/go/bin
export GOROOT_BOOTSTRAP=$HOME/.golang/go1.4

golang实现tcp反代/转发

package main

import (
    "bytes"
    "flag"
    "io"
    "log"
    "net"
    "os"
    "strings"
    "time"
)

type formatter struct {
    w      io.Writer
    offset uint
    prefix string
    tstamp bool
}

func min(a, b int) int {
    if a < b {
        return a
    }

    return b
}

func (f *formatter) Write(p []byte) (int, error) {
    var completedBytes int
    for i := 0; i < len(p); i += 16 {
        m := min(len(p), i+16)
        err := f.format(p[i:m])
        if err != nil {
            return completedBytes, err
        }
        completedBytes += m - i
        f.offset += uint(m - i)
    }
    return len(p), nil
}

var hex = []byte("0123456789abcdef")

func (f *formatter) format(buf []byte) error {

    // prefix addr:_(hex dump)+spaces+space+bar+chars+bar+newline

    // our line is 83 characters of formatting

    plen := len(f.prefix)

    if f.tstamp {
        plen += len(time.Stamp)
    }

    llen := 83 + plen

    ptr := 0

    line := make([]byte, llen)
    if f.tstamp {
        s := time.Now().Format(time.Stamp)
        copy(line, []byte(s))
        ptr += len(time.Stamp)
    }

    copy(line[ptr:], []byte(f.prefix))
    ptr += len(f.prefix)

    offs := f.offset

    ptr += 8

    line[ptr] = ':'
    ptr--

    for offs > 0 {
        line[ptr] = hex[offs&0x0f]
        ptr--
        offs >>= 4
    }

    for ptr >= plen {
        line[ptr] = '0'
        ptr--
    }

    ptr = plen + 9
    line[ptr] = ' '
    ptr++

    for i, b := range buf {
        if i%4 == 0 {
            line[ptr] = ' '
            ptr++
        }

        line[ptr] = hex[b>>4]
        ptr++
        line[ptr] = hex[b&0x0f]
        ptr++
        line[ptr] = ' '
        ptr++

    }

    // fill in rest of line
    for i := len(buf); i < 16; i++ {
        if i%4 == 0 {
            line[ptr] = ' '
            ptr++
        }

        line[ptr] = ' '
        ptr++
        line[ptr] = ' '
        ptr++
        line[ptr] = ' '
        ptr++
    }

    line[ptr] = ' '
    ptr++
    line[ptr] = ' '
    ptr++
    line[ptr] = '|'
    ptr++

    for _, v := range buf {
        if v > 32 && v < 127 {
            line[ptr] = v
        } else {
            line[ptr] = '.'
        }
        ptr++
    }

    line[ptr] = '|'
    ptr++

    line[ptr] = '\n'
    ptr++

    _, err := f.w.Write(line[:ptr])
    return err
}

func copyStream(conn io.WriteCloser, r io.Reader) {
    _, err := io.Copy(conn, r)
    if err != nil {
        log.Printf("error during copy: %v\n", err)
    }
    err = conn.Close()
    if err != nil {
        log.Printf("error during close: %v\n", err)
    }
}

func main() {

    proxy := flag.String("p", "9999:blog.bbzhh.com:80", "proxy line -- <lport>:<rhost>:<rport>")
    tstamps := flag.Bool("t", false, "add time-stamps when proxying")
    debug := flag.Bool("d", false, "print debug information when proxying")

    flag.Parse()

    // provided a proxy line
    if *proxy != "" {
        pieces := strings.Split(*proxy, ":")
        dst := pieces[1] + ":" + pieces[2]

        fprefix := "<= "
        tprefix := "=> "

        if *tstamps {
            fprefix = " <= "
            tprefix = " => "
        }
        var (
            fin  io.Writer
            fout io.Writer
        )
        if *debug {
            fin = &formatter{os.Stdout, 0, fprefix, *tstamps}
            fout = &formatter{os.Stdout, 0, tprefix, *tstamps}
        } else {
            buf := bytes.NewBuffer(make([]byte, 0))
            fin = &formatter{buf, 0, fprefix, *tstamps}
            fout = &formatter{buf, 0, tprefix, *tstamps}
        }

        ln, e := net.Listen("tcp", ":"+pieces[0])
        if e != nil {
            log.Fatal("listen error:", e)
        }

        log.Println("tcp server starting")

        for {
            lconn, err := ln.Accept()
            if err != nil {
                log.Println(err)
                continue
            }

            go func(lconn net.Conn) {
                tl := io.TeeReader(lconn, fout)
                rconn, err := net.Dial("tcp", dst)
                if err != nil {
                    log.Println("error connectiong to", dst, ":", err)
                    err := lconn.Close()
                    if err != nil {
                        log.Printf("error closing connection: %v\n", err)
                    }
                    return
                }
                tr := io.TeeReader(rconn, fin)
                go copyStream(rconn, tl)
                go copyStream(lconn, tr)
            }(lconn)
        }
    }

    if *tstamps {
        log.Println("-t only applies when proxying, ignoring")
    }

    fout := &formatter{os.Stdout, 0, "", false}

    var fin io.Reader

    // process stdin
    if flag.NArg() == 0 {
        fin = os.Stdin
    } else {
        fname := flag.Arg(0)
        var err error
        fin, err = os.Open(fname)
        if err != nil {
            log.Fatal(err)
            return
        }
    }

    _, err := io.Copy(fout, fin)
    if err != nil {
        log.Printf("error during copy: %v\n", err)
    }
}

tornado微信公众号

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

__author__ = 'tan9le'

import os
from wechatpy import parse_message, create_reply
from wechatpy.utils import check_signature
from wechatpy.exceptions import (
    InvalidSignatureException,
)
import schedule
from datetime import timedelta
import tornado.wsgi
from tornado.options import define, options

TOKEN = os.getenv('WECHAT_TOKEN', '填写token')
AES_KEY = os.getenv('WECHAT_AES_KEY', '填写EncodingAESKey(消息加解密密钥)')
APPID = os.getenv('WECHAT_APPID', '填写AppID(应用ID)')

define("port", default=8000, help="run on the given port", type=int)
class WeiXinHandler(tornado.web.RequestHandler):
    #用于微信公众号网页修改基本信息时的验证
    def get(self):
        # 获取微信公众平台发送的验证参数
        signature = self.get_argument('signature', '')
        timestamp = self.get_argument('timestamp', '')
        nonce = self.get_argument('nonce', '')
        echostr = self.get_argument('echostr', '')
        try:
            check_signature(TOKEN, signature, timestamp, nonce)
        except InvalidSignatureException:
            self.set_stauts('403')
            self.write('error,code 403')
        #按照约定,如果正常,则原样返回echostr
        self.write(echostr)
    def post(self):
        # 获取微信公众平台发送的验证参数
        signature = self.get_argument('signature', '')
        timestamp = self.get_argument('timestamp', '')
        nonce = self.get_argument('nonce', '')
        echostr = self.get_argument('echostr', '')
        # 获取所有值并解析
        msg = parse_message(self.request.body)
        if msg.type == 'text':
            content = msg.content.strip()
            
            reply = create_reply(content, msg)
            self.write(reply.render())
        else:
            help_str = "目前仅支持文字输入\n"
            reply = create_reply(help_str, msg)
            self.write(reply.render())




app = tornado.wsgi.WSGIApplication([
    # 这里需要根据修改为自己的URL匹配
    (r"/", WeiXinHandler),

])

if __name__=="__main__":
    # 启动tornado实例
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

然后使用

python main.py --port=8000

就可以启动应用啦
依赖:

tornado
wechatpy

C# 获取所有用户进程信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Management;
using System.Collections;
using System.Net;

namespace WindowsFormsTestApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Console.Write("窗体加载");
        }

        private static string GetProcessUserName(int processId)
        {
            string name = "";
           
            SelectQuery query = new SelectQuery("select * from Win32_Process where processID=" + processId);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

            try
            {
                foreach (ManagementObject disk in searcher.Get())
                {
                    ManagementBaseObject inPar = null;
                    ManagementBaseObject outPar = null;

                    inPar = disk.GetMethodParameters("GetOwner");

                    outPar = disk.InvokeMethod("GetOwner", inPar, null);

                    name = outPar["User"].ToString();
                    break;
                }
            }
            catch
            {
                name = "SYSTEM";
            }

            return name;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string str = "";
            textBox1.Clear();
            Process[] processes;
            processes = Process.GetProcesses();
            //Grab some basic information for each process.
            Process process;
            List<IDictionary> infoList = new List<IDictionary>();
            Dictionary<string, Object> result = new Dictionary<string, Object>();
            for (int i = 0; i < processes.Length - 1; i++)
            {
                process = processes[i];
                textBox1.AppendText(Convert.ToString(process.Id) + " : " + process.ProcessName + " : " + GetProcessUserName(process.Id) + "\r\n");
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("pid", Convert.ToString(process.Id));
                dic.Add("name", process.ProcessName);
                dic.Add("userName", GetProcessUserName(process.Id));
                infoList.Add(dic);
            }
            result.Add("list", infoList);
            IPHostEntry myHost = new IPHostEntry();
            myHost = Dns.GetHostByName(Dns.GetHostName());
            result.Add("hostName", myHost.HostName.ToString());
            string myip = "";
            for (int i = 0; i < myHost.AddressList.Length; i++)
            {
                if(myip.Length == 0)
                {
                    myip = myip + myHost.AddressList[i].ToString();
                }
                else
                {
                    myip = myip + ";" + myHost.AddressList[i].ToString();
                }
               
            }
            result.Add("ipaddr", myip);
            MessageBox.Show("检测已完成,总计读取到"+ infoList.Count+ "个进程" + JsonConverter.Serialize(result));
        }
    }
}