Docker and she was so fun? Acting to build commercial 4G combat!

How time flies, from the previous article in this series, "commercial grade 4G agency set up to prepare guidelines [Articles]" released time has passed for two weeks, last week due to various tedious things, there is no start Tuesday empty write articles, so the Gugu Gu.

Then prepare papers, we understand what the agent set up the hardware and software needed for 4G, but also know the advantages and disadvantages of various options. Now, we can start the actual build, we also believe that the long-awaited.


The basic idea

We can see from the title of this article, this time the program is mainly used to build a Docker, you may be very curious, Docker with the agency to build 4G have anything to do?

Ah, a great relationship, we comb the whole thing, take a look at the basic process to build 4G Agent:

  1. Calling card dialing, it will create a virtual network card after dialing success. (Use the virtual card under normal circumstances, be able to access)

  2. In the case of multi-card, repeat the first step, you will get more virtual NICs.

  3. Start the proxy server to use as a virtual network card network card, network card and use an entity within the access network as a network card.

    Such use is almost

But then, there is a problem, according to test results before me, the current in the Linux environment does not have a HTTP proxy server can be done separately specify a network card and a network card, which is very troublesome ah ... such a problem because if we can not do so, there will be similar to the following:

  1. Both the network and the network on a virtual network card, you must use a proxy server to go public network access.
  2. LAN network for the entity, but the network is locked to a particular proxy server, unable to take advantage of multiple network adapters.

Ah ... so do not HTTP proxy server, use those often used to make some show of operating Socks5 proxy server? If you can specify if the network card, and then tools like Privoxy as the HTTP proxy Socks5 proxy turn into just fine. (A well-known Windows version of the software supporting the wall is so turn HTTP proxy)

After some attempts, though I found some documents Socks5 proxy server is to say you can specify the card, but after following the instructions, do not seem to directly effect what I want (either above or locked in one, either not on the Internet), so there are some problems. You may need to meet the routing table is set up to operate it, but my understanding of network engineering is not very deep, do a few days did not engage them, and thus have to think of something.

That's when I thought of a thing --Docker, it can be used to solve this problem!

Docker because the container is created, no matter how many external network card, network card inside the container there will be only a Docker own virtual LAN (used for communication between the container) and a local loopback interfaces (do not ignore it), and we when dialing operation within the container, the new virtual network adapter created will not affect the outside world or other containers, so, you do not need to specify proxy server NIC, will be able to directly start to run!

So now ran through the entire process into actual operational links to see it!


Systems

Docker way to build this version, many aspects of the selection system, because the sample device I am using a Raspberry Pi, so here chose to use Raspbian (raspberry pie exclusive version of Debian). If you are using another device, then choose a direct their common system just fine.

The first step of course is ready after the first download and install Docker, here I use the one-click install scripts directly Docker official to be installed:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
# 出自官方文档:https://docs.docker.com/install/linux/docker-ce/debian/#install-using-the-convenience-script
复制代码

The one-click installation script theory, all Linux distributions can be used, after all, has been out for a long time, if not, then use their own search engines to find relevant information.

Once installed Docker, you have two options:

  1. Experience into the mode, find out the specific details of the operation is kind of how.
  2. Do not look at this passage, turn to the bottom of this article I have written the most direct use of the wheel.

Start container

Experience, we would direct such a Docker container start it, execute the following command:

sudo docker run -it --rm --privileged -p 3128:3128 ubuntu:18.04 bash
复制代码

Meaning above command is to start an internal system for the container Ubuntu18.04 and into the interior of the container Shell execute bashcommand, if you exit the bash will automatically destroy the container; and then map the port in the container 3128 to the outside world, mapping out the outside world port 3128 is; last privilegedparameter is turned privileged mode, the apparatus for mapping the card into the container.

If you download the image is slow, you can search about "Docker accelerator" can also be directly supporting the wall.

To test the card is normal

After entering the interior of the container, we can perform what ls /dev/ttyUSB*look at the card has not properly been recognized (outside the container is the same, because the opened privileged mode), if it is, and I bought with a 4G network card, then inserted only a in the case of the card you will see four ttyUSB equipment.

Insert the card looks like a three, a total of 12 ttyUSB equipment

4G network cards and different hardware combinations may vary, subject to the actual situation.

If you can see 4✖4G网卡个数a ttyUSB equipment, then it shows no problem, you can begin the next step.

online

The next thing to do is dial up, dial aspects Wvdial can choose to use this tool, you can also choose to use dial-up scripting Fanconn such as a vendor-supplied (direct call PPPD), use of the effect there will be some differences. If the business does not provide dial-up script, then it Wvdial use it, it can automatically generate configuration, namely to get started with.

My side, then because Fanconn technicians provide direct dial-up script, I'll use this script, Wvdial documents online there are many very detailed, there is not more to mention, a friend in need on their own search.

If you are using a dial-up Fanconn this script (how to get into the container would not I say it?), Then directly apt install pppafter installing dial-up tool, used chmod +x quectel-pppd.shfor dial-up script to add a permission to run, then ./quectel-pppd.sh /dev/ttyUSB3you can.

Used when dialing /dev/ttyUSB3refers to the interpretation of the fourth card 4G communication ports, the document is: ttyUSB3 → For PPP connections or AT command communication, translate what is, for PPP connections or AT command communication.

Ifconfig after dialing with a tool similar to the state seen in FIG:

It can be seen as mentioned earlier, there are now three cards, one Docker own, a local loopback interfaces (that do not control), a virtual network card dialing generated.

If not used within Docker container, then there will be a wwan0 (or other name), that is 4G card body.

Test whether normal Internet

Now if you use the curl of --interfacea specified virtual network adapter parameters, then the request (eg: curl --interface ppp0 https://ip.cn), is already a successful request, IP is what you will use the SIM card corresponding operator assigned.

Since Docker mirror are usually extremely streamlined, and therefore there is no pre Ubuntu mirror image net-tools, iputils-ping, vim, curl or the like of these packages need to install. So if you find ifconfig, ping, curl, vim not take, do not panic, this is normal, execute apt install 包名the command to install.

If you can not directly request is successful, then it may be a DNS resolution problem, you can try to ping a public IP (eg: ping 1.1.1.1) and a domain name (eg: ping ip.cn), if the IP can ping but the domain name will be reported DNS resolution fails, you can confirm that the DNS settings problem.

If the DNS settings when dialing 4G problems, usually because dial-up tool does not normally return to the operators of the DNS server settings are written to the configuration, we can manually configure the look (you want to force a particular DNS can specify):

# 以下为阿里云的公共DNS
echo 'nameserver 223.5.5.5' >> /etc/resolv.conf
echo 'nameserver 223.6.6.6' >> /etc/resolv.conf
复制代码

In Docker container, this /etc/resolv.confdocument may also have two contents, the container itself is required, it is recommended not to remove / cover, otherwise the situation can not communicate with each other using the name of the container between the container will appear.

Start Proxy

So after testing 4G card by dialing indeed after the Internet, we can put a proxy server started, I used here is TinyProxy.

Discover the test, Squid greater occupation of some resources, is not conducive to use in multi-card situation, it will affect the maximum number of 4G network card.

The first apt install tinyproxywave, and then vim /etc/tinyproxy/tinyproxy.confmodify the configuration.

To modify the configuration are:

  • Port configuration item changed to 3128, we've mapped out because the port is 3128.
  • Listen changed configuration items 0.0.0.0, because we need to use this proxy server on other devices.
  • Allow configuration item comment out or changed to 0.0.0.0/0, default 127.0.0.1 cause other devices can not access.

After he saved a wave, then you can execute directly tinyproxyinitiated ... yet?

And so on, there is an operational need to do! That is the default route to point to a virtual network card, very simple, execute the following command:

route del -net 0.0.0.0 eth0
route add -net 0.0.0.0 ppp0
复制代码

The meaning of these two commands are: first default, eth0 pointing to this card access route to delete, and then add one and the same, the route to ppp0 this card.

After the effect of changing the default route is finished, curl, even if you do not use --interfaceparameters, can also be used directly 4G card online.

If you do not change the default routing, in case it does not specify card, 4G card and will not be used to, because the default route points to Docker own virtual network card, network card that leads to your original network environment. In other words, IP will not change!

So now, you can execute tinyproxyto start the proxy server.

Testing proxy server

Well, the proxy server should have a normal start, and now we can try to connect the container proxy server, see if you can use it properly by Internet 4G network card on another device.

For example, I here Raspberry Pi assigned IP is: 192.168.137.66, then I can be tested using this curl command or Python code:

curl:

curl "https://ip.cn"
curl -x "192.168.137.66:3128" "https://ip.cn"
复制代码

Python:

import requests
resp = requests.get("https://ip.cn", proxies={"https": "http://192.168.137.66:3128"})
no_proxy_resp = requests.get("https://ip.cn")
print(resp.text)
print(no_proxy_resp.text)
复制代码

The test result should coincides with the previous test in the interior of the container after use proxy IP becomes IP base station operator assigned.

Replace the IP

So the core question is, how to replace the IP it?

In fact, those who use dial-up and VPS set up a proxy server, we only need to dial a number will be able to change the IP directly kill off pppd process can make it disconnect dialing, re-dialing script is executed again after re-dial the disconnection.

Disconnect dial aspects Fanconn technical staff also provides a script, also in chmod +x quectel-ppp-killafter giving permission to run, do ./quectel-ppp-killit.

But it notes that the dial-up cellular network after disconnection, IP will still be retained for a period of time (do not know specifically how long, probably connected with the base station also has a relationship), so we need to make a mandatory re-search the network card.

Popular Tips: Turn the phone off flight mode effect is to re-search the network, usually just close the "mobile data", the result is consistent with the disconnect dialing.

How to do it? Very simple, two-line command:

AT+CFUN=0
AT+CFUN=1
复制代码

But note well, this is AT command, not a Linux Shell command under, modem AT commands is a command language, if we need to execute it up, you need to do:

echo "AT+CFUN=0" > /dev/ttyUSB2
# 中间间隔1秒左右
echo "AT+CFUN=1" > /dev/ttyUSB2
复制代码

As used herein, /dev/ttyUSB2refers to the interpretation of the third card 4G communication ports, the document is: ttyUSB2 → For AT command communication, communication with the fourth port is similar, except that it can not be used PPP connection, AT commands can only be used for communication only.

The fourth reason for not using the same communications port is the port there is the possibility of being occupied, separate direct safest area, which is the original card provides two AT command communication channels.

In the card a few seconds after the re-search time within the network to ten / tens of seconds, you can not dial properly, you need to wait before it can dial a successful initialization is complete, specific waiting time for signal strength prevail, I tested when usually within 5 seconds on it.

So if you have dial-up failure after disconnection, you may wish to try again after a while.


to sum up

So now operational processes also run through, and we also understand that the entire internal details, the last thing to do is put each card is assigned a container, respectively, so that we can achieve at the beginning of the article mentioned - "virtual network card use as a network card, and the card within the access network using the physical effect as a network interface "of the.

Actual operation, then, is the designated part of the network card to configure, then passed just fine when starting a container using a Docker container environment variable settings can easily achieve this function.

Finally, this idea that we can build a docker-compose template, template First, the core content to be a simple container cluster 4G network card, the second is to start a Squid, for aggregation proxy server, so we only need to specify the use of a proxy server can replace the random operation more convenient.


Well, above is the Docker version built way of thinking and the whole building process, if you bother to look at it directly with my written wheels is also possible, just send a message [Docker 4G version of Agent] to the public No. [NightTeam] It can be.

Evaluation

Finally, the last, I set up this way to make a review!

This is not the way to build perfect, because too many variables, but certainly not as many places the system-level native support less stable, long-term use may appear all kinds of weird problems.

Then Docker's footprint is actually good, but will waste a considerable amount of memory on the boot container, if only twenty-three NIC Fortunately, if the number of bigger words, like the Raspberry Pi such a small memory device 2B simply carry do not live.

In addition to the proxy server itself consumes resources is relatively high, high frequency calls for Raspberry Pi 2B small CPU pressure is still quite large, even though I know it's been overclocked CPU, still appears relaxed when concurrent test played CPU usage.

but! Up to now, there are two I plan to build a router-based system did not write it! So ... stay tuned follow-up program to build other (strabismus laughs).


Author of the article: "Team NightTeam night" - Loco

Night team was founded in 2019, the team includes CUI Qing Cai, Zhou Ziqi, Chenxiang An, Tang Yifei, Feng Wei, Cai Jin, Dai Huang Jin, Zhang Ye Qing and Weishi Dong.

Dabbled in programming languages ​​include, but are not limited to, Python, Rust, C ++, Go, areas covered reptile, deep learning, service development, the object storage. Team non-positive nor evil, only thought was right, please be careful.

Guess you like

Origin juejin.im/post/5d7f0993e51d4561c94b106e