Connect via a SOCKS server in golang

The function below allows you to setup a SOCKS connection in golang. You should already have connected to the SOCKS server using net.Dial. The function will then send the required header to connect to the target address/port provides in address:port format. For example:

conn, err = net.Dial("tcp", "socksproxy:8080")
err = socks_connect(conn, "hosttoconnectto:80")

You can then communicate with the target server via the SOCKS proxy.

func socks_connect(conn net.Conn, address string) (error) {

        host, portstr, err := net.SplitHostPort(address)
        port, err2 := strconv.Atoi(portstr)
        if err2 != nil {
          return errors.New("Unable to parse port")
        }

        version := []byte{0x04} // socks version 4
        cmd := []byte{0x01}     // socks stream mode
        buffer := bytes.NewBuffer([]byte{})
        binary.Write(buffer, binary.BigEndian, version)
        binary.Write(buffer, binary.BigEndian, cmd)
        binary.Write(buffer, binary.BigEndian, uint16(port))
        binary.Write(buffer, binary.BigEndian, []byte{0x00, 0x00, 0x00, 0x01})
        binary.Write(buffer, binary.BigEndian, []byte{0x00})
        binary.Write(buffer, binary.BigEndian, []byte(host))
        binary.Write(buffer, binary.BigEndian, []byte{0x00})
        binary.Write(conn, binary.BigEndian, buffer.Bytes())

        data := make([]byte, 8) // socks responses are 8 bytes
        count, err := conn.Read(data)

        if err != nil {
          return errors.New("Unable to connect to socks server.")
        }
        if count == 0 {
          return errors.New("Unable to connect to socks server.")
        }
        if data[1] == 0x5a { // success
                return nil
        }

        return errors.New("Unable to connect to socks server.")
}

USRP1 simple data capture in C++ example (UHD)

The code below streams data using the UHD at 1MSPS. It just prints the first 2 datapoints to the screen but can be used as the basis for other stuff. It’s also worth checking out rx_samples_to_file.cpp in the UHD source, but this example removes a lot of the cruft. Can be compiled with:

g++ rx.cpp -luhd -o rx
#include <uhd/types/tune_request.hpp>
#include <uhd/utils/thread_priority.hpp>
#include <uhd/utils/safe_main.hpp>
#include <uhd/usrp/multi_usrp.hpp>
#include <uhd/exception.hpp>
#include <iostream>
#include <fstream>
#include <csignal>
#include <complex>

using namespace std;

int UHD_SAFE_MAIN(int argc, char *argv[]){
    uhd::set_thread_priority_safe();

    uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(string(""));

    usrp->set_clock_source("internal"); // set clock

    //always select the subdevice first, the channel mapping affects the other settings
    usrp->set_rx_subdev_spec(string("A:A"));

    std::cout << "Using Device: " << usrp->get_pp_string() << std::endl;
    double rate = 1000000; // trying 1MSPS
    usrp->set_rx_rate(rate);

    //set the center frequency
    double freq=0;
    uhd::tune_request_t tune_request(freq);
    usrp->set_rx_freq(tune_request);

    //usrp->set_rx_gain(5);
    //usrp->set_rx_bandwidth(100);

//    ant = "";
//    usrp->set_rx_antenna(ant);

//   cout << usrp->get_mboard_sensor("lo_locked", 0).to_bool() << endl;

    sleep(1);

/////////////// RECEIVE CODE

    //create a receive streamer
    uhd::stream_args_t stream_args("sc16","");
    uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);

    uhd::rx_metadata_t md;
    std::vector<std::complex<short> > buffer(50000);

    //setup streaming
    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
    stream_cmd.num_samps = 0;
    stream_cmd.stream_now = true;
    stream_cmd.time_spec = uhd::time_spec_t();
    rx_stream->issue_stream_cmd(stream_cmd);

    for(;;) {

        size_t num_rx_samps = rx_stream->recv(&buffer.front(), buffer.size(), md, 3.0, false);

        if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) { cerr << "timeout"  << endl; }
        if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW){ cerr << "overflow" << endl; }
        if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE)    { cerr <<  md.strerror() << endl; }

        cout << "data size: " << num_rx_samps << " first: " << (&buffer.front())[0] << endl;
    }

    // Will never get here
    stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;
    rx_stream->issue_stream_cmd(stream_cmd);
}

ESP8266 – esp1 PCB scans

There are some partial layout images for the esp1 but they seem incomplete. I wanted to make sure I knew exactly what was going on with this layout and as they’re so cheap decided to pull the components off a board and scan it. Actually a little pointless, as I was wondering if there was a trace snaking under the chip, but nope! Anyway, for reference, here are the scan images:

esp1_1

esp1_2

esp1_pcb

This is a continuation of my notes on the esp8266 microcontroller, you can find a complete list of my esp8266 posts here

Portable electronics toolkit

When I’m working on site I generally carry a small electronics toolkit with me. This is my current kit. I’m mostly using Hozan and Hakko tools.

kit

While these are really great tools, they are a little expensive (about 20USD for the sidecutters). I’d like to put a bunch of these kits together using cheaper alternate tools. The important points for me are:

Side Cutters

hozanN34

A nice flat edge is probably the most important point.

Wire strippers

P967

The P967s go from 18 to 28AWG solid core, or 20 to 30 stranded. I much prefer these fixed wire cutters to self adjusting tools, they never seem to work very well.

Loupe

A loupe is pretty important for inspecting small parts, or reading codes off SMD packages. Mine is x8 and x10 which seems fine. I’d like to find one that was a little better this has significant chromatic aberration. The triplet loupes seem better.

Soldering Iron

I use an Hakko FX888 on my bench, and an FX600 in my kit. The FX600 is actually a little bulky and I don’t always carry it, but it uses the same tips as th FX888 (and a bunch of other Hakko compatible irons) so you can use nice high quality tips with it.

Tweezers

Tweezers are a whole world unto themselves. I don’t feel qualified to write in detail about tweezers here. The image below shows a selection:

tweezers

The top set are labeled “Rhino Switzerland Standard SW-11”, they are cheap Shenzhen specials. But they’re pretty good, I think they cost about 1USD. At the ultralow end there’s a huge variation in quality. You’ll need to shop around, but these are reasonable everyday tweezers particularly if you don’t want to worry about being too careful with them. They’re fine for general work, placing SMD components etc.

Below that we have idealtek 2AMZ.SA ceramic tip tweezers. Ceramic tips are nice, they can withstand high temperature without warping and don’t conduct heat too well. These tweezers have a huge tip, they’re good for picking up larger components connected to a ground plain.

The next 2 tweezers are Rubis TNFs. Rubis generally manufacture tweezers for the cosmetics industry! But I think these are aimed more at the engineering/manufacturing market and they seem to have gained a following here in Japan. The “TNF” means Titanium. As such they are able withstand very high temperatures, and come to a fine point (unlike the ceramic tips). The slim line TNF tweezers (SS-TNF) are my current favorite.

The Hozan P-89s are nothing special. These are inverted tweezers which can be used for holding wires in place etc. Handy to carry around to help you clamp something in place while soldering.

Other tools

The wire is pretty standard wirewrap and hookup wire. Calipers are handy. The Pilers are unserrated and used to bending component legs etc.. Solder is 60/40 fine gauge (0.35mm I think), I should also keep some larger gauge for through hole stuff. Solder braid is pretty standard, as is the Flux pen.

kit2

kit3

kit4