5. Given $a, b \in \mathbf{R}$, the function $f(x) = ax - b$. If for any $x \in [-1, 1]$, we have $0 \leqslant f(x) \leqslant 1$, then the range of $\frac{3a + b + 1}{a + 2b - 2}$ is ( ).
(A) $\left[-\frac{1}{2}, 0\right]$
(B) $\left[-\frac{4}{5}, 0\right]$
(C) $\left[-\frac{1}{2}, \frac{2}{7}\right]$
(D) $\left[-\frac{4}{5}, \frac{2}{7}\right]$
5. D.

From the problem, we have
$$
\begin{array}{l}
\left\{\begin{array} { l } 
{ 0 \leqslant f ( 1 ) \leqslant 1 , } \\
{ 0 \leqslant f ( - 1 ) \leqslant 1 }
\end{array} \Leftrightarrow \left\{\begin{array}{l}
0 \leqslant a-b \leqslant 1, \\
-1 \leqslant a+b \leqslant 0 .
\end{array}\right.\right. \\
\text { Let } u=a+b, v=a-b .
\end{array}
$$

Then $a=\frac{u+v}{2}, b=\frac{u-v}{2}$.
Thus $\frac{3 a+b+1}{a+2 b-2}=\frac{4 u+2 v+2}{3 u-v-4}$
$$
\begin{array}{l}
=-2+\frac{10}{3-\left(\frac{5 v-11}{5 u-3}\right)} \\
\Rightarrow-\frac{4}{5} \leqslant \frac{3 a+b+1}{a+2 b-2} \leqslant \frac{2}{7} .
\end{array}
$$