Network Programming with Go teaches you how to write clean, secure network software with the programming language designed to make it seem easy.
Go combines the best parts of many other programming languages. It’s fast, scalable, and designed for high-performance networking and multiprocessing—in other words, it’s perfect for network programming.
Network Programming with Go is for developers ready to start leveraging Go’s ease of use for writing secure, readable, production-ready network code. Early chapters establish a foundation of networking and traffic-routing know-how upon which the rest of the book builds. You’ll put that knowledge to use as author Adam Woodbeck guides you through writing programs that communicate using TCP, UDP, Unix sockets, and other features that ensure reliable data transmission. As you progress, you’ll explore higher-level network protocols like HTTP and HTTP/2, then build applications that securely interact with servers, clients, and APIs over a network using TLS.
In addition, Woodbeck shows you how to create a simple messaging protocol, develop tools for monitoring network traffic, craft a custom web server, and implement best practices for interacting with cloud providers using their SDKs. Along the way, you’ll learn:
• IP basics for writing effective network programs, such as IPv4 and IPv6 multicasting, ports, and network address translation
• How to use handlers, middleware, and multiplexers to build capable HTTP-based applications with minimal code
• The OSI and TCP/IP models for layered data architectures
• Methods for reading data from/writing data to a network connection, like the type-length-value encoding scheme
• Tools for incorporating authentication and encryption into your applications using TLS, like mutual authentication
• How to serialize data for storage or transmission in Go-friendly formats like JSON, Gob, XML, and protocol buffers
• How to Leverage Go’s code generation support to efficiently communicate with gRPC-based network services
So get ready to take advantage of Go’s built-in concurrency, rapid compiling, and rich standard library. Because when it comes to writing robust network programs, it’s Go time.
Author(s): Adam Woodbeck
Edition: 1
Publisher: No Starch Press
Year: 2021
Language: English
Commentary: Vector PDF
Pages: 392
City: San Francisco, CA
Tags: Google Cloud Platform; Microsoft Azure; Cloud Computing; Web Programming; Reliability; DNS; HTTP; Logging; Serialization; OSI Model; Application Development; Network Programming; Go; Web Servers; TCP/IP; IPv6; AWS Lambda; Network Address Translation; Internet Protocol; UDP; Routing; Network Layers; TLS; Metrics; Internet Control Message Protocol; Unix Domain Sockets
Acknowledgments
Introduction
Who This Book Is For
Installing Go
Recommended Development Environments
What’s in This Book
Part I: Network Architecture
Chapter 1: An Overview of Networked Systems
Choosing a Network Topology
Bandwidth vs. Latency
The Open Systems Interconnection Reference Model
The Hierarchal Layers of the OSI Reference Model
Sending Traffic by Using Data Encapsulation
The TCP/IP Model
The Application Layer
The Transport Layer
The Internet Layer
The Link Layer
What You’ve Learned
Chapter 2: Resource Location and Traffic Routing
The Internet Protocol
IPv4 Addressing
Network and Host IDs
Subdividing IPv4 Addresses into Subnets
Ports and Socket Addresses
Network Address Translation
Unicasting, Multicasting, and Broadcasting
Resolving the MAC Address to a Physical Network Connection
IPv6 Addressing
Writing IPv6 Addresses
IPv6 Address Categories
Advantages of IPv6 over IPv4
The Internet Control Message Protocol
Internet Traffic Routing
Routing Protocols
The Border Gateway Protocol
Name and Address Resolution
Domain Name Resource Records
Multicast DNS
Privacy and Security Considerations of DNS Queries
What You’ve Learned
Part II: Socket-level Programming
Chapter 3: Reliable TCP Data Streams
What Makes TCP Reliable?
Working with TCP Sessions
Establishing a Session with the TCP Handshake
Acknowledging Receipt of Packets by Using Their Sequence Numbers
Receive Buffers and Window Sizes
Gracefully Terminating TCP Sessions
Handling Less Graceful Terminations
Establishing a TCP Connection by Using Go’s Standard Library
Binding, Listening for, and Accepting Connections
Establishing a Connection with a Server
Implementing Deadlines
What You’ve Learned
Chapter 4: Sending TCP Data
Using the net.Conn Interface
Sending and Receiving Data
Reading Data into a Fixed Buffer
Delimited Reading by Using a Scanner
Dynamically Allocating the Buffer Size
Handling Errors While Reading and Writing Data
Creating Robust Network Applications by Using the io Package
Proxying Data Between Connections
Monitoring a Network Connection
Pinging a Host in ICMP-Filtered Environments
Exploring Go’s TCPConn Object
Controlling Keepalive Messages
Handling Pending Data on Close
Overriding Default Receive and Send Buffers
Solving Common Go TCP Network Problems
Zero Window Errors
Sockets Stuck in the CLOSE_WAIT State
What You’ve Learned
Chapter 5: Unreliable UDP Communication
Using UDP: Simple and Unreliable
Sending and Receiving UDP Data
Using a UDP Echo Server
Receiving Data from the Echo Server
Every UDP Connection Is a Listener
Using net.Conn in UDP
Avoiding Fragmentation
What You’ve Learned
Chapter 6: Ensuring UDP Reliability
Reliable File Transfers Using TFTP
TFTP Types
Read Requests
Data Packets
Acknowledgments
Handling Errors
The TFTP Server
Writing the Server Code
Handling Read Requests
Starting the Server
Downloading Files over UDP
What You’ve Learned
Chapter 7: Unix Domain Sockets
What Are Unix Domain Sockets?
Binding to Unix Domain Socket Files
Changing a Socket File’s Ownership and Permissions
Understanding Unix Domain Socket Types
Writing a Service That Authenticates Clients
Requesting Peer Credentials
Writing the Service
Testing the Service with Netcat
What You’ve Learned
Part III: Application-level Programming
Chapter 8: Writing HTTP Clients
Understanding the Basics of HTTP
Uniform Resource Locators
Client Resource Requests
Server Responses
From Request to Rendered Page
Retrieving Web Resources in Go
Using Go’s Default HTTP Client
Closing the Response Body
Implementing Time-outs and Cancellations
Disabling Persistent TCP Connections
Posting Data over HTTP
Posting JSON to a Web Server
Posting a Multipart Form with Attached Files
What You’ve Learned
Chapter 9: Building HTTP Services
The Anatomy of a Go HTTP Server
Clients Don’t Respect Your Time
Adding TLS Support
Handlers
Test Your Handlers with httptest
How You Write the Response Matters
Any Type Can Be a Handler
Injecting Dependencies into Handlers
Middleware
Timing Out Slow Clients
Protecting Sensitive Files
Multiplexers
HTTP/2 Server Pushes
Pushing Resources to the Client
Don’t Be Too Pushy
What You’ve Learned
Chapter 10: Caddy: A Contemporary Web Server
What Is Caddy?
Let’s Encrypt Integration
How Does Caddy Fit into the Equation?
Retrieving Caddy
Downloading Caddy
Building Caddy from Source Code
Running and Configuring Caddy
Modifying Caddy’s Configuration in Real Time
Storing the Configuration in a File
Extending Caddy with Modules and Adapters
Writing a Configuration Adapter
Writing a Restrict Prefix Middleware Module
Injecting Your Module into Caddy
Reverse-Proxying Requests to a Backend Web Service
Creating a Simple Backend Web Service
Setting Up Caddy’s Configuration
Adding a Reverse-Proxy to Your Service
Serving Static Files
Checking Your Work
Adding Automatic HTTPS
What You’ve Learned
Chapter 11: Securing Communications with TLS
A Closer Look at Transport Layer Security
Forward Secrecy
In Certificate Authorities We Trust
How to Compromise TLS
Protecting Data in Transit
Client-side TLS
TLS over TCP
Server-side TLS
Certificate Pinning
Mutual TLS Authentication
Generating Certificates for Authentication
Implementing Mutual TLS
What You’ve Learned
Part IV: Service Architecture
Chapter 12: Data Serialization
Serializing Objects
JSON
Gob
Protocol Buffers
Transmitting Serialized Objects
Connecting Services with gRPC
Creating a TLS-Enabled gRPC Server
Creating a gRPC Client to Test the Server
What You’ve Learned
Chapter 13: Logging and Metrics
Event Logging
The log Package
Leveled Log Entries
Structured Logging
Scaling Up with Wide Event Logging
Log Rotation with Lumberjack
Instrumenting Your Code
Setup
Counters
Gauges
Histograms and Summaries
Instrumenting a Basic HTTP Server
What You’ve Learned
Chapter 14: Moving to the Cloud
Laying Some Groundwork
AWS Lambda
Installing the AWS Command Line Interface
Configuring the CLI
Creating a Role
Defining an AWS Lambda Function
Compiling, Packaging, and Deploying Your Function
Testing Your AWS Lambda Function
Google Cloud Functions
Installing the Google Cloud Software Development Kit
Initializing the Google Cloud SDK
Enable Billing and Cloud Functions
Defining a Cloud Function
Deploying Your Cloud Function
Testing Your Google Cloud Function
Azure Functions
Installing the Azure Command Line Interface
Configuring the Azure CLI
Installing Azure Functions Core Tools
Creating a Custom Handler
Defining a Custom Handler
Locally Testing the Custom Handler
Deploying the Custom Handler
Testing the Custom Handler
What You’ve Learned
Index