This is the fourth day of my participation in the August Text Challenge.More challenges in August

preface

Recently in the development and wechat small program, public number related services. Then, the painful life began……

  • The access_token can be invoked only after the IP address is configured
  • To receive messages from the wechat server, you also need to use an external IP address

In this case, we need to deploy the server after writing locally. Look at the log again, change the code again, deploy again, and look at……

Alternatively, write 🤣 directly on the server

But they want to develop happily locally. So what to do?

My thinking is as follows:

👉 You can access local services using the server IP address

👉 The server forwards local requests

The IP address of the server accesses local services

This section requires Intranet penetration tools. There are many online, some are paid and some are free.

FRP is used here and configured on the server.

Post an introduction to FRP

FRP is a high-performance reverse proxy application that focuses on Intranet penetration and supports various protocols such as TCP, UDP, HTTP, and HTTPS. Intranet services can be exposed to the public network in a secure and convenient way through the transfer of nodes with public IP addresses.

Download the FRP

Wget HTTP: / / https://github.com/fatedier/frp/releases/download/v0.37.1/frp_0.37.1_linux_amd64.tar.gzCopy the code

Unpack the FRP

The tar - XVF frp_0. 37.1 _linux_amd64. Tar. GzCopy the code

Set up the service

vim /usr/lib/systemd/system/frp.service
Copy the code

Write the following

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.ini
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
StandardOutput=syslog
StandardError=inherit

[Install]
WantedBy=multi-user.target
Copy the code

Note: /usr/local/frp/ in ExecStart is the installation path of the FRP. If your installation path is different from this one, change it to the corresponding one

Reload the service

systemctl daemon-reload
Copy the code

Add automatic startup

systemctl enable frp
Copy the code

Modify the FRps.ini file in the FRP installation directory

[common]
bind_port = 7000      Port for connecting the FRP client to the server
vhost_http_port = 80  ## Listen on port
Copy the code

Notice The firewall port of the server needs to be enabled

Starting the server

systemctl start frp
Copy the code

Access when no local services are running

Configure the FRP client

The system corresponding to the client is being downloaded in Releases.

Windows_amd64 downloaded locally

Modify the frpc.ini configuration file after installation

[common] server_addr = Server IP server_port = 7000 [web]type= HTTP local_port = 8080 custom_Domains = Domain name or server IP addressCopy the code

After this configuration, when both the SERVER and client of the FRP are enabled. We can access the localhost:8080 service through server IP :80.

Note: If you also use this for wechat development, you can only set the server listening port to 80 or 443.

FRP has many other Settings that you can explore for yourself 😉

The server forwards local requests

Wechat only receives requests from IP addresses in the IP whitelist, so we can’t get relevant data when we initiate a request locally.

Let’s start by adding our server IP to the whitelist.

In this case, we are forwarding our requests through Nginx

Server Settings

Install Nginx using Docker

docker pull nginx:latest
Copy the code

Start Nginx because port 80 has been used, change to port 8080

docker run --name nginx-redirect -p 8080:80 -d  nginx
Copy the code

Copy the nginx configuration file from the container

 docker cp nginx-redirect:/etc/nginx/* /usr/local/nginx-docker/conf/
Copy the code

Pause and delete the newly started nginx container

docker stop nginx-redirect
docker rm nginx-redirect
Copy the code

Recreate the nginx container and map the nginx configuration files

docker run --name nginx-redirect -p 8080:80 -d -v /usr/local/nginx-docker/conf:/etc/nginx nginx
Copy the code

/usr/local/nginx-docker-conf = /usr/local/ nginx-docker-conf = /usr/local/ nginx-docker-conf = /usr/local/ nginx-docker-conf

cd /usr/local/nginx-docker/conf
vim nginx.conf
Copy the code

Add a server to the HTTP block of the nginx.conf file

When nginx receives a request that matches the /cgi-bin/ path, it forwards the request to api.weixin.qq.com

server{ listen 80; Server_name 127.0.0.1; location /cgi-bin/{ proxy_pass https://api.weixin.qq.com; }}Copy the code
docker exec -it nginx-redirect bash  ## Enter the container
nginx -s reload						 ## Enable the configuration file
Copy the code

Client Setup

Take the access_token as an example. Here is the SpringBoot project

Because the final deployment to the server, we do not need to forward through Nginx, so for the development interface of wechat, we use configuration injection

Generate a configuration class

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/** * wechat public API constant class */
@Data
@Configuration
@ConfigurationProperties(prefix = "project.wechat-constant")
public class WechatOfficialAccountApiUrlConsts {

    private String accessTokenApi;

}
Copy the code

Application. Yml configuration

There is a little bit of a pitfall here. Don’t use ACCESS_TOKEN_API in your configuration, it will not be detected

project:
  wechat-constant:
    access-token-api: http://server IP :8080/cgi-bin/token? Grant_type =client_credential& appID =APPID&secret=APPSECRET
Copy the code

After this configuration, when we request an access_token, we first send a request to the server, and then nginx on the server forwards the request to api.weixin.qq.com

Other requests can be set with similar configuration.

conclusion

With the above configuration, we can happily develop locally.

Make fun of 😤 : wechat interface with good uncomfortable.

reference

Nginx configures several methods for forwarding domain names to other domain names

CentOS7 Builds Frp to access web services deployed on the Intranet